I have a class defined above int main, and i have a class in a .h/.cpp format. I have the #include "blah.h" in my main .cpp file. But i can not use the classes above int main in the .h file.
for example...if i wanted to pass the class above int main to the class in my .h file.
bool class1::touches(class2* b)
{
etc...
}
Where class1 is in the .h, and class2 is above main...i get class2 is an undeclared identifier. and b is an undeclared identifier.
does the .h file also #include "blah.h"? that could be your problem, although if your class is declared above the line that #includes "blah.h" in your main.cpp file then it should compile and you should be able to use the class in your .h file. but if your .h file is at the top of the main.cpp file and above your class declaration, then it will not compile. remember, the #include statement acts as if yo replace that line where #include occurs, with the entire contents of the file.
You have to move the class2(class from main.cpp file) to separate header file(for example class2.h), after that you need to include class2.h in your file where the compiler error is situated
After re rereading my post I think I could be a little more clear. here is an example of what I think is happening in your program:
main.cpp:
1 2 3 4 5 6 7 8 9 10 11
#include "class1.h" /* any classes declared in class1.h, or any classes defined in main.cpp above this line, will not have access to any constants, variables or classes declared below this line*/
class class2{
//class 2 member & functions go here
};
int main()
{
//your program goes here
}
class1.h:
1 2 3 4 5 6 7 8 9 10 11
#ifndef CLASS1_H
#define CLASS1_H
class class1{
//constructors, destructor, function & data memers go here
bool class1::touches(class2* b){} /*Since class2 has not been declared yet (notice in the main.cpp file, the class2 definition occurs after the line #include "class1.h") you will get a compile error here*/
};
//if its a 2 part file then uncomment the line below:
//#include "class1.cpp
#endif
You could do what dennis said, but be sure that the class that calls the other class, is included in a line after the first class in the main.cpp, otherwise you will still get the same error.
so something like this:
1 2
#include "class2.h"//this needs to be declared first,
#include "class1.h"//in order for class1 to have access to it