basicly. I created the definition of a "rational" at rational.h and rational.cpp
I compiled it and got rational.o.
I then try to use the class in another .cpp file called test.cpp
I used #include "rational.h"
then i wrote rational r; in the main function.
But the compiler complains of undefined reference to `rational::rational()' which i clearly defined.
and I put those two files in the same folder. So now, i don't know how to "help" the test.cpp to "find" the definitions now.
the thing is, the book i'm learning from taught about this problem throughtout the chapters i was reading. But now i don't know what to do with this simple case.
to filipe: i tried that as well. didn't work. so that's probably not the problem
to firedraco: the file is below. I basicly copied it from the book i'm learning from
the compiler didn't complain when i compiled it with the rational.cpp file.
#include <string>
class rational {
public :
//constructors
rational ();
rational (int , int);
rational (const rational&);
yeah, i did define all my functions in the rational.cpp file. I mostly just copied it from a book.
Could it be because i'm using code::block to compile it or sth? b/c i can't think of anything else. According to the book, everything should work out, then it doesn't. So frustrating.
i just got it to work by including a #include "rational.cpp" statement in my test.cpp program.
but i thought i just need to include the .h file.
am i supposed to include the rational.cpp file in the rational.h file??? Come to think of it, how else would the compiler now there is a rational.cpp file.
But somehow i don't remember the books ever doing it that way. They always write the .cpp, which include the header. but the header doesn't include the .cpp. and in the tester program, they include the .H file. This doesn't make any sense. is the compiler supposed to magically find the .cpp file?
You shouldn't include .cpp files. The header file should contain everything a user of the class will need.
how else would the compiler now there is a rational.cpp file.
The compiler will first compile each .cpp file separately. Header files will only compile if they're included in a .cpp file. Note that #include simply copies and pastes the included file into the .cpp file. At this stage, the compiler only checks that every identifier used is declared, but not necessarily defined.
After each unit is compiled into object code (they're called translation units) the linker will link the files. That's when it will need to find a definition for every identifier used.
after the compiler sees that the contents of rational.h.
it either 1) search for a rational.cpp file , which i think is pretty smart
2) compiles ALL the cpp files in the same folder, and look for the functions that need to be defined
(which doesn't seem smart, what if there's a thousand files in the same folder?)
3) but neither of the above makes sense, because i got errors if i don't include the .cpp. And further, i think it makes every logical sense to "directly lead" the compiler to find the .cpp file with the include. because, as you said, the include copies the file over, which is supposed to be the point of separating different parts of the program into different files.
why doesn't the .cpp get included manually, and instead we ask the compiler to search for it? this seems illogical and still magically to me.
thank you for your help, i'm new to this, and i find this very "simple (as the tone suggest in the books and tutorials) " concept very very confusing.
Well, it happens as I described it. If, say, rational.h is included in main.cpp, the compiler will not look at rational.cpp. The linker will look for the definitions of the functions described in rational.h in any of the translation units; it doesn't matter if they're implemented in a file called rational.cpp or foo.cpp or whatever.
Would you care to post your rational.cpp using [ code ] [ /code ] tags (without the spaces)?
I just used dev-c++ to try to compile. didn't work either.
how do i "indicate" to the dev-c++ or code::block that the three files are in the same project? I thought it was enough that they be in the same folder.
EDIT: i got it to compile now in dev-c++ by adding them into the same project.
but i still want to learn the non-IDE way to do it. Can you recommend any compiler for windows that would do it?
to filipe: thanks, i thinking i'm getting the idea now.
to Disch: thank you soo much for your three line example. often, i can't think straight when i'm frustrated, and i don't know how long it would take me to figure out the proper way to compile and link the program. thank you again.