Hello people.
Who can tell me, how to include header and cpp files correctly? How do not repeat or multiply generating code? I use (win32) Dev-C++ compiler. When i put reader.cpp file content to reader.h it's compile and work correctly. But i know, that class description have to be in header file then other class code like realization i mean class functions, constructors should be in cpp file. So code splited below don't compile, and i get erros like: 1 ...\reader.cpp `Reader' has not been declared
2 ...\reader.cpp ISO C++ forbids declaration of `Reader' with no type
...\reader.cpp In function `int Reader(int)':
3 ...\reader.cpp `itsNr' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
At global scope:
6 ...\reader.cpp expected constructor, destructor, or type conversion before '::' token
And etc.
In general, it is a bad idea to include .cpp files.
In your example above, your reader.cpp will need to #include "reader.h" , since reader.h is where the forward declarations of the functions defined in reader.cpp reside, and is what is causing most of your errors on compilation.
I deleted from main.cpp my #include "reader.h" and included in to reader.cpp
Well Desh by your observation most errors gone. Thank you a lot!
Compiler drops only 6 ...\main.cpp `Student' undeclared (first use this function) error.
By this error i understand that now i need to declare my object, but it's declared directly below the class in reader.h with default parameter. In global my object can't be. Any recommends?
What is up with line 16 in your header? Why are there parentheses after your object name? In any case, you should be including "reader.h" in main()'s file and declare Reader Student; inside the main() function.
Mw3284 Thank you very much, I will have that in mind.
Maybe you can recommend what to do with my obect now, which is decleared in my reader.h, but compiler can't see my object?
Ok Zhuge, by your opinion i declared my class object in main.cpp and i got this compiler errors:
4 ...\reader.cpp redefinition of `Reader::Reader(int)'
21 ...\reader.h `Reader::Reader(int)' previously defined here
9 ...\reader.cpp redefinition of `Reader::~Reader()'
26 ...\reader.h `Reader::~Reader()' previously defined here
...\reader.cpp In member function `void Reader::regMeniu()':
17 ...\reader.cpp `Student' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
...\Makefile.win [Build Error] [reader.o] Error 1
Did you include the header guards recommended by mw3284? And you should not be using Student on line 17 in your source file. Either you made a silly mistake or don't understand OOP fully yet; that method should be modifying that object that is calling it, not an arbitrary other object.
Zhuge, well i don't understand OOP fully yet... i'm learning now so thats why i asking maybe my silly mistakes.
I included in header guards recommended by mw3284. Also I found some problems, that i need to declare my object in to other header and to include it to my reader.h file. But it's not work yet. Maybe silly mistake... Searching for my mistake yet...
Glad to hear your problem is solved, however I would again recommend not #including .cpp files, or any files other than header files. I have observed time and time again that this is a bad practice to get into. Can anyone with more experience back me up on this?