class "SomeClass" hasn't been declared , but I included it's header !

HI Everyone , this is a piece of my code (in StudentBinary.h) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef STUDENTBINARY_H_INCLUDED
#define STUDENTBINARY_H_INCLUDED

#include "Student.h"

class StuBinary
{
private :
    char * FirstName ;
    char * LastName ;
public :
    //StuBinary (Student input) ;
    void Change (Student input) ;
} ;

#endif // STUDENTBINARY_H_INCLUDED 


When I try to build it , an error appears :
line 13 -> Student hasn't been declared .

BUT as you see I have included "Student.h" !!!!



And this is another piece of my code in another file and another function :
1
2
3
4
5
6
7
for (it = stus.begin () ; it != stus.end () ; it ++)
    {
        tmp = * it ; //it is an iterator of 'vector <Student>' and tmp is an object of class 'Student'
        StuBinary Btmp ;
        Btmp.Change (tmp) ;
        stubin.write ((char *) & Btmp , 40) ; //stubin is a binary file
    }


A strange error appears here too :
line 5 -> no matching function for call to 'StuBinary :: Change (Student &)
candidate is : void StuBinary :: Change (int)
no known conversion for argument 1 from 'Studnet' to 'int'

Why is that ?
I haven't any 'Change (int)' anywhere .

THNX!
Last edited on
does Student.h include StudentBinary.h?
Thank you @coder777
yes it does include , I erased it and now there's no problem .
Can you please tell me what causes that ?

BUT I need StudentBinary.h in Student.h

Because there will also be a 'Change' for converting StuBinary to Student .
Now wha tshould I do?

Thnak so much .
I still nEEd answer !
Sorry for spam .
the topic was down
Can you please tell me what causes that ?
Well: Student.h includes StudentBinary.h includes Student.h includes StudentBinary.h and so on. You see that it cannot work

Now wha tshould I do?
The solution is forward declaration:

http://en.wikipedia.org/wiki/Forward_declaration

and using references instead of copies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef STUDENTBINARY_H_INCLUDED
#define STUDENTBINARY_H_INCLUDED

#include "Student.h"

class Student; // forward declaration

class StuBinary
{
private :
    char * FirstName ;
    char * LastName ;
public :
    //StuBinary (const Student &input) ;
    void Change (const Student &input) ; // as a reference not a copy
} ;

#endif // STUDENTBINARY_H_INCLUDED 


#include "Student.h" just in the implementation (cpp) file
Thank you!
But another question :
U said "Student.h includes StudentBinary.h includes Student.h includes StudentBinary.h and so on"
But aren't those "#ifndef"s and "#define"s and "endif"s used top prevent that?
Thanks !
But aren't those "#ifndef"s and "#define"s and "endif"s used top prevent that?
Yes, they are called include guards (by the way)

The problem is that when StudentBinary.h is already include in Student.h then all that StudentBinary.h sees from Student.h is an empty file.
That's the reason why the compiler complains about not knowing Student at this point
Topic archived. No new replies allowed.