These classes are co-dependant on each other i.e they both call functions that reside in the other. Now do you see those namespaced variables A:j and B:i (they are both on line 9 of each header file, both set to an int of 12)? If I comment them out the code runs as expected:
1 2 3 4 5 6 7
int main() {
A::foo(); // --> outputs A::foo called
// B::bar called
// B::baz called
// B::qux called
}
However, the second I uncomment those namespaced variables I get this:
1 2 3 4 5 6 7 8 9
CMakeFiles/Inheritance.dir/A.cpp.o:A.cpp:(.data+0x0): multiple definition of `B::i'
CMakeFiles/Inheritance.dir/main.cpp.o:main.cpp:(.data+0x0): first defined here
CMakeFiles/Inheritance.dir/A.cpp.o:A.cpp:(.data+0x4): multiple definition of `A::j'
CMakeFiles/Inheritance.dir/main.cpp.o:main.cpp:(.data+0x4): first defined here
CMakeFiles/Inheritance.dir/B.cpp.o:B.cpp:(.data+0x0): multiple definition of `A::j'
CMakeFiles/Inheritance.dir/main.cpp.o:main.cpp:(.data+0x4): first defined here
CMakeFiles/Inheritance.dir/B.cpp.o:B.cpp:(.data+0x4): multiple definition of `B::i'
CMakeFiles/Inheritance.dir/main.cpp.o:main.cpp:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
How should I stucture my code so two namespaces can call on each other but their variables are only set once and so my code compiles? Should I turn one of them into a class? Worth noting I am pretty new to C++
Most variables that are not inline may not be defined in a header file. Because the contents of each header file are blindly pasted in every source file that contains it, the situation here is as-if you tried to link two source files that each declared the same symbols.
To declare a non-const namespace-scope variable in a header file, define it inline (since C++17), or declare it extern and define that variable in exactly one source file.
In your case: inlineint i = 12;
Don't rely on transitive includes:
In a header file, include each header that is required for the header file, and nothing more.
In a source file, include the corresponding header first, followed by every header that is required for the source file, and nothing more.