I have classes called janitor, lunch, drink, and school. In the member functions that take classes as parameters, the compiler says that some of the classes are not declared. I cannot figure out why since I included the header files which contain the classes. I only pasted the 4 header files, I don't think anyone would need the .cpp's or the main. Error list is at the bottom.
In file included from janitor.h:7,
from drink.h:4,
from drink.cpp:1:
lunch.h:16: error: âjanitorâ has not been declared
lunch.h:16: error: âdrinkâ has not been declared
In file included from drink.h:4,
from drink.cpp:1:
janitor.h:20: error: âdrinkâ has not been declared
In file included from lunch.h:6,
from janitor.h:7,
from driver.cpp:3:
drink.h:15: error: âjanitorâ has not been declared
drink.h:15: error: âLunchâ has not been declared
In file included from janitor.h:7,
from driver.cpp:3:
lunch.h:16: error: âjanitorâ has not been declared
In file included from lunch.h:6,
from janitor.h:7,
from janitor.cpp:1:
drink.h:15: error: âjanitorâ has not been declared
drink.h:15: error: âLunchâ has not been declared
In file included from janitor.h:7,
from janitor.cpp:1:
lunch.h:16: error: âjanitorâ has not been declared
lunch.h: In function âvoid rand_move(school&, janitor&, Lunch&, drink&)â:
lunch.h:19: error: âlocation Lunch::LunchLocâ is private
janitor.cpp:24: error: within this context
lunch.h:19: error: âlocation Lunch::LunchLocâ is private
janitor.cpp:52: error: within this context
lunch.h:20: error: âchar Lunch::LunchRepâ is private
janitor.cpp:52: error: within this context
In file included from janitor.h:8,
from lunch.h:5,
from lunch.cpp:1:
drink.h:15: error: âjanitorâ has not been declared
drink.h:15: error: âLunchâ has not been declared
In file included from lunch.h:5,
from lunch.cpp:1:
janitor.h:20: error: âLunchâ has not been declared
You have a circular dependency. janitor.h includes lunch.h. lunch.h includes janitor.h, but the header guard prevents janitor.h from being included and lunch.h fails because it depends on what is in janitor.h.
To prevent this problem you can use forward declaration.
Instead of #include "janitor.h" you write class janitor; This will work as long as all you have is pointers or reference to janitor objects. If you use the object you will need to include the header so in the source files you will probably have to include them. Do the same with all your other headers, not just janitor.h. A good rule is to use forward declarations if it's enough and only include if you have to.
I was going to say yes but now I see you are passing location by value and have it as a member variable so you will need the class definition to be able to do that. What you could do is put location in a separate header and include it where you need it and use forward declarations on the rest.