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

This is my code:

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.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
a
#include "game.h"
#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;
}


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
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>


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 


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
#include "game.h"
#include "human.h"
#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);
}


human.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef HUMAN_H_INCLUDED
#define HUMAN_H_INCLUDED
#include "game.h"
#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 


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
#include "game.h"
#include "AI.h"
#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);
}


AI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef AI_H_INCLUDED
#define AI_H_INCLUDED
#include "game.h"
#include <string>

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

        void AImove();

    private:
        string choice;
};


#endif // AI_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
#include "game.h"
#include "ref.h"
#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;
}


ref.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef REF_H_INCLUDED
#define REF_H_INCLUDED
#include "game.h"
#include <string>

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

        string getResult();

    private:
        string result;
};

#endif // REF_H_INCLUDED 


The error:
1
2
3
4
5
6
7
8
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\Asus\Desktop\Coding Works\Cdoubleplus\Year 2\Prac 2\Submission\main.o:main.cpp|| undefined reference to `human::human(std::string)'|
C:\Users\Asus\Desktop\Coding Works\Cdoubleplus\Year 2\Prac 2\Submission\main.o:main.cpp|| undefined reference to `AI::AI()'|
C:\Users\Asus\Desktop\Coding Works\Cdoubleplus\Year 2\Prac 2\Submission\main.o:main.cpp|| undefined reference to `AI::AImove()'|
C:\Users\Asus\Desktop\Coding Works\Cdoubleplus\Year 2\Prac 2\Submission\main.o:main.cpp|| undefined reference to `Ref::Ref()'|
C:\Users\Asus\Desktop\Coding Works\Cdoubleplus\Year 2\Prac 2\Submission\main.o:main.cpp|| undefined reference to `Ref::getResult()'|
||error: ld returned 1 exit status|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 
Last edited on
Basically a duplicate topic: http://www.cplusplus.com/forum/general/220025/
Topic archived. No new replies allowed.