I have one class, called A, and it has it's own header file. Then I have another class, called B, which also has it's own header file. They each have their own .cpp file where I implement all of their functions.
I'm trying to have class B have a variable of class type A as a private variable, but I keep getting the error 'A' does not name a type
My code looks like this:
main.h:
#ifndef MAIN_H #define MAIN_H #include "A.h" #include "B.h" #endif main.cpp:
#include "main.h" int main( int argc, char* args[]) { B test; } A.h:
#ifndef A_H #define A_H #include "main.h" class A { public: //public functions private: //private variables }; #endif B.h:
#ifndef B_H #define B_H #include "main.h" class B { public: //public functions... private: A temp; } #endif So all of my includes are in main.h, which includes A before B. B has a variable of type A, but it is included from being in main.h and B.h includes main.h. However, I keep getting an error saying:
error: 'A' does not name a type. I've done some googling, and it seems like that means that A isn't defined when you use it, but it should be defined there since it's being included in main.h, right?
B.hinmain.cppandA.hinB.h? Include only the things you need. I am curious if your compiler is getting confused by the include recursion.main.h->A.h->main.h, etc.