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!
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace std;
usingnamespace 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
staticint nchoice; //number of RPS game human played
static string hchoice; //human choice
static string cchoice; //computer choice
private:
};
#endif // GAME_H_INCLUDED
#include "game.h"
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace 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);
}
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
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include "human.h"
#include "AI.h"
#include "ref.h"
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace std;
usingnamespace 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
staticint 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.
#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
staticint nchoice; //number of RPS game human played
static std::string hchoice; //human choice
static std::string cchoice; //computer choice
};
#endif // GAME_H_INCLUDED