Circular include. Expected name-class before '{' token

Hi, I'm new to programming. I'm trying to make a RPS game but I'm having trouble with the circular include. I will post all of my code here. Thank you!

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "game.h"
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()  {
    string input_string;
   // cout<<"Enter string: "<<endl;
    getline(cin,input_string);

    human human_player(input_string);
  //  cout<<"Human choice: "<<human_player.getHchoice()<<endl;
    AI ai_player;
    ai_player.AImove();
    Ref referee;
    cout<<referee.getResult()<<endl;
}


game.h

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
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

using namespace std;

class human;
class AI;
class Ref;

class game  {
    public:
        newGame();

        void setHchoice(string Hchoice);
        void setCchoice(string Cchoice);
        string getHchoice ();
        string getCchoice ();
        void setNchoice(int n);
        int getNchoice();

    protected: //game is used as a storage of data from the AI and human
        static int nchoice; //number of RPS game human played
        static string hchoice; //human choice
        static string cchoice; //computer choice

    private:


};

#endif // GAME_H_INCLUDED 


game.cpp

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
#include "game.h"
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
game::newGame() {
    hchoice="";
    cchoice="";
}

int game::nchoice=0;
string game::hchoice="";
string game::cchoice="";

void game::setHchoice(string Hchoice) {
    hchoice=Hchoice;
}

void game::setCchoice(string Cchoice)  {
    cchoice=Cchoice;
}

string game::getHchoice ()   {
    return hchoice;
}

string game::getCchoice ()  {
    return cchoice;
}

void game::setNchoice(int n) {
    nchoice=n;
}

int game::getNchoice()  {
    return nchoice;
}

human.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef HUMAN_H_INCLUDED
#define HUMAN_H_INCLUDED
#include "game.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
class human : public game   {
    public:
        human();
        human(string choice);

        void setchoice(string Choice);

    private:
        string choice;
        int nchoice;
};

#endif // HUMAN_H_INCLUDED 

human.cpp

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
47
48
49
50
51
52
53
54
55
#include "game.h"
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

human::human()  {
    choice="";
    setHchoice("");
    setNchoice(0);
}

human::human(string Choice)    {
    int i;
    int nchoice=0;
    choice=Choice;
    setHchoice(Choice);
    for (i=0;i<=Choice.length();i++) { //counting the number of game the human made
        if (Choice[i]==32)  {
            nchoice++;
        }
    }

    if  (nchoice!=0)    {
        nchoice++;
    }

    if (nchoice==0 && Choice[0]!=0) {
        nchoice=1;
    }

    setNchoice(nchoice);
    //cout<<"n in human: "<<getNchoice()<<endl;
}

void human::setchoice(string Choice)   {
    int i;
    int nchoice=0;
    choice=Choice;
    setHchoice(Choice);
    for (i=0;i<Choice.length();i++) {
        if (Choice[i]==32)  {
            nchoice++;
        }
    }

    if  (nchoice!=0)    {
        nchoice++;
    }
    setNchoice(nchoice);
}


AI.h
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
#ifndef AI_H_INCLUDED
#define AI_H_INCLUDED
#include "game.h"
#include "human.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

using namespace std;
class AI : public game  {
    public:
        AI();

        void AImove();

    private:
        string choice;
};


#endif // AI_H_INCLUDED

AI.cpp
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
#include "game.h"
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
AI::AI()    {
    setCchoice("");
}

void AI::AImove()   {
    int n=getNchoice();
    int i;
    int k;

   // cout<<"n in AImove: "<<n<<endl;

    for (i=0;i<n;i++)   {
        //cout<<"i: "<<i<<endl;
        k=(rand()%3)+1;
        //cout<<"k: "<<k<<endl;
        if (k==1)    {
            choice=choice + "R ";
        }
        else if (k==2)    {
            choice=choice + "P ";
        }
        else if (k==3)    {
            choice=choice + "S ";
        }
    }
    setCchoice(choice);
}

ref.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef REF_H_INCLUDED
#define REF_H_INCLUDED
#include "game.h"
#include "human.h"
#include "AI.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

using namespace std;
class Ref : public game { //THE ERROR OCCURS HERE
    public:
        Ref();

        string getResult();

    private:
        string result;
};

#endif // REF_H_INCLUDED 

ref.cpp
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
47
48
49
50
51
52
53
54
55
56
57
#include "game.h"
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
Ref::Ref()  {
    result="";
}

string Ref::getResult()    {
    string human;
    string ai;
    int i;
    result="";

    human=getHchoice();
    ai=getCchoice();

    for (i=0;i<human.length();i++)  {   //comparing 2 choice from human and computer
        if  (human[i]!=32)  {
            if (human[i]==82)   { //=R
                if (ai[i]==82)  {   //=R
                    result=result+"T ";
                } else if   (ai[i]==80) { //=P
                    result=result+"L ";
                } else if   (ai[i]==83) { //=S
                    result=result+"W ";
                }
            }

            else if (human[i]==80)   { //=P
                if (ai[i]==80)  {   //=P
                    result=result+"T ";
                }   else if (ai[i]==82) {   //=R
                    result=result+"W ";
                }   else if (ai[i]==83) {   //=S
                    result=result+"L ";
                }
            }

            else if (human[i]==83)  {   //=S
                if (ai[i]==83)  {   //=S
                    result=result+"T ";
                } else if (ai[i]==82)  {    //=R
                    result=result+"L ";
                } else if (ai[i]==80)   {   //=P
                    result=result+"W ";
                }
            }
        }
    }
    return result;
}


Last edited on
closed account (48T7M4Gy)
https://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies
Please post the complete error messages, all of them, exactly as they appear in your development environment. Your error messages have important information embedded within them to aid in locating and fixing the problems.

It appears to me that your including way too many include directives in your files. You should only #include necessary includes not everything. For example your game.h
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
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

using namespace std;

class human;
class AI;
class Ref;

class game  {
    public:
        newGame();

        void setHchoice(string Hchoice);
        void setCchoice(string Cchoice);
        string getHchoice ();
        string getCchoice ();
        void setNchoice(int n);
        int getNchoice();

    protected: //game is used as a storage of data from the AI and human
        static int nchoice; //number of RPS game human played
        static string hchoice; //human choice
        static string cchoice; //computer choice

    private:


};

#endif // GAME_H_INCLUDED  

This file should probably only need the <string> header file.

Also note that you shouldn't be using the "using" clause in a header file, instead you should properly scope the items in the std::namespace, and you should properly const qualify several of your member functions.

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

#include <string>

class game  {
    public:
        newGame(); // This function needs a return type, only constructors can omit the return type.

        void setHchoice(const std::string& Hchoice);
        void setCchoice(const std::string& Cchoice);
        std::string getHchoice () const;
        std::string getCchoice () const;
        void setNchoice(int n);
        int getNchoice() const;

    protected: //game is used as a storage of data from the AI and human
        static int nchoice; //number of RPS game human played
        static std::string hchoice; //human choice
        static std::string cchoice; //computer choice
};

#endif // GAME_H_INCLUDED  



Topic archived. No new replies allowed.