I’m getting confused with my C++ class structure below:
A.h:
class A{
};
A.cpp
//implementation for A
void A::someMethod();
B.cpp:
#include "A.h"
B : public A{
//stuff for B
};
int main{
B b();
b.someMethod();
}
g++ A.cpp A.h –> compiles fine
g++ B.cpp –> undefined reference to main
You need to do:
To generate A.o and B.o (without
-cg++ expects to link and produce a complete executable – and expects amainfunction, whereas -c specifies compile and assemble but do not link).You can then link the two object files into an executable using:
Also: do not specify the header files on the command line. You need to specify them using
#includedirectives in your cpp files and then the compiler will pull their contents in directly as the pre-processing stage of compiling each cpp. If you’re interested in the details of the stages of compilation for C++, Microsoft have detailed those for their compiler here.