compiler says my class is not declared

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.

drink:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef DRINK_H
#define DRINK_H

#include "janitor.h"
#include "school.h"
#include <fstream>
#include "lunch.h"

class drink
{
  public:
    drink();
    void place_me(school & skool);
    friend ostream& operator<<(ostream& fout, const drink & drank);
    friend void rand_move(school & skool, janitor & jan, Lunch & lunch, drink & drank);
  private:
    string Descr;
    float effect;
    location DrinkLoc;
    char DrinkRep;

};
#endif 



Lunch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef LUNCH_H
#define LUNCH_H

#include "school.h"
#include "janitor.h"
#include "drink.h"
#include <iostream>
using namespace std;

class Lunch
{
  public:
   Lunch(const char LunchChar = 'L'):LunchLoc(-1, -1), LunchRep(LunchChar){}

   void place_me(school & skool);
   friend void rand_move(school & skool, janitor & jan, Lunch & lunch, drink & drank);
   //void rand_move(school & skool);
  private:
    location LunchLoc;
    char LunchRep;
};
#endif 


janitor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef JANITOR_H
#define JANITOR_H

#include <iostream>
#include <string>
#include "school.h"
#include "lunch.h"
#include "drink.h"
using namespace std;

class janitor
{
  public:
   janitor (string name, char janchar = 'J')
   :bac(.05), Bruisecount(0), JanLoc(-1, -1), jancharacter(janchar),
   sober(true), dead(false), jan_name(name){}


  void place_me(school & skool);
  friend void rand_move(school & skool, janitor & jan, Lunch & lunch, drink & drank);


  friend ostream& operator<<(ostream& fout, const janitor & jan);

  private:
    float bac;
    int Bruisecount;
    char jancharacter;
    bool sober;
    bool dead;
    string jan_name;
    location JanLoc;
};
#endif 


school:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef SCHOOL_H
#define SCHOOL_H

#include <iostream>
#include <cstdlib>
using namespace std;

const int MAX = 25;
const int Size = 10;
const int MAX_DRINKS = 100;
const short SPACING = 4;
const char EMPTY = ' ', WALL = 'B', WINDOW = 'F', ERROR = '\0';;
enum Directions {DOWN, RIGHT, UP, LEFT, NUM_DIR};

struct location
{
  location():m_Xval(0), m_Yval(0){}
  location(const short X, const short Y):m_Xval(X), m_Yval(Y){}
  location FindAdjacent() const;
  
  int m_Xval;
  int m_Yval;
};


class school
{
  public:
    school(const int gridSize = MAX, short WindowSpacing = SPACING);

    char GetNewPosition(const location Loc) const;
    void SetNewPosition(const location Loc, const char Representation);
    location GetStartingPoint() const;
    friend ostream& operator<<(ostream& fout, const school & skool);
    //Get function
    int getSize()const {return ActualSize;}

  private:
    void build(const short WindowSpacing);
    bool InsideSchool(const location Loc) const;
    void clear();

    char School[MAX][MAX];
    short ActualSize;
};
#endif 


Here is the error list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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.
Last edited on
So everywhere I #include a different header file, I should change to a forward declaration??
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.
Last edited on
I'm pretty sure that's NOT what you said, but it was a plea for clarification while attempting to reduce the risk of sounding stupid.
alright, I'll try that
It compiled and works fine, thanks for your help.
Topic archived. No new replies allowed.