I think I\'m getting close, but I\'m having this error I\'ve been banging my head against the wall on for hours. I\'m missing something stupid, and I\'ve gone character by c
The command line for g++
needs to include both source files, like this:
g++ -g -Wall main.cpp translator.cpp -o Pear
Otherwise, the compiler has no idea from where to get the implementation of the translator::translator(std::istream&)
member function.
*EDIT: * (from the comment)
I thought that basically the use of header files was so that it would know where to get each implementation of the file?
This part is grossly oversimplified, but it should help you get the picture. Recall that the process of producing an executable from C++ sources consists of two major steps - compilation and linking. The g++
program performs them both (it can do just one if you specify -c
flags, or pass only .o
files).
The compiler and the linker stages of g++
do not "talk" to each other directly. The compiler produces the inputs for the linker, and that's where the communication ends.
Header files are for the compiler. Specifically, they are for the first stage of compilation - the preprocessing. Once the preprocessor has finished, there is no knowledge of where the definitions came from. Even the compiler does not know it, let alone the linker. That is why you need to "help" the linker by supplying all the relevant sources to g++
.
You aren't linking translator.o with your application.
g++ -g -c translator.cpp
followed by
g++ -g -Wall main.cpp translator.o -o Pear