Plz help! How to include correctly???

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//reader.h
class Reader
{
      int itsNr;
      
      public:
             
             Reader(int defNr);
             ~Reader();
             
             double getNr() {return itsNr;}
             
             void setNr(int xNr) { itsNr = xNr;}

             void regMeniu();
} Student (7);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//reader.cpp
Reader::Reader(int defNr)
{     
      itsNr = defNr;
}

Reader::~Reader()
{
}

void Reader::regMeniu()
{
     int fsNr;
     std::cout << "Enter new reader number\n";
     std::cin >> fsNr;
     Student.setNr(fsNr);
}


1
2
3
4
5
6
7
8
9
10
11
//main.cpp
#include <iostream>
#include "reader.h"

int main()
{	
	Student.regMeniu();
	std::cout << "Reader number is: " << Student.getNr();
	
	return 0;
}


Maybe when i split, then it's need to declare class object to global or smth that?

Sorry for my english mistakes. Hope you can help me.
closed account (Lv0f92yv)
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.

Your main.cpp looks OK to me.
Last edited on
in the header file:
1
2
3
4
#ifndef READER_H
#define READER_H
//header file
#endif  

in reader.cpp:
1
2
#include <iostream> //you are using cout statements in your file
#include "reader.h" 

main.cpp is correct with:
1
2
#include <iostream>
#include "reader.h" 
Last edited on
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?
Last edited on
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


Maybe something more is not included?
Last edited on
if you want to use your constructor then:
Reader r1(44);
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...
Last edited on
Looks like the problem i solved! Thanks everybody 4 help!
closed account (Lv0f92yv)
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?

Cheers.
Topic archived. No new replies allowed.