为什么会出现这个未定义的参考?我已经包含了我需要的所有头文件。
#include <iostream>
#include <string>
#include <cstdlib>
#include "Player.h"
#include "Team.h"
using namespace std;
int main()
{
char a;
Team t1("Maxites", 0, 0, 0);
Team t2("Diasoimoi", 0, 0, 0);
// Starting Menu choices.
cout << "Starting Menu ... (choose between a-d)"<<endl;
cout << "a.Add Player to a team"<< endl << "b.View details of Team" << endl;
cout << "c.View data of a Player either by name or by position in the table. " <<endl;
cout << "d.Exit program" << endl;
cin >> a;
for (int i=10; i>0; i--)
{
if ((a!='a') && (a!='b') && (a!='c') && (a!='d'))
{
cout << "Please press only a, b, c or d from keyboard " << i << " tries left..." << endl;
cin >> a;
}
}
switch(a)
{
case 'a':
{
int be;
cout << "Select Team of the new player: Press 1 for " << t1.nameT << " or 2 for " << t2.nameT << endl;
cin >> be;
for (int i=10; i>0; i++)
{
if ((be!=1 && be!=2))
{
cout << "Please press only 1 or 2 from keyboard" << i << "tries left..." << endl;
}
}
switch(be)
{
case 1:
{
t1.addplayer();
break;
}
case 2:
{
t2.addplayer();
break;
}
}
break;
}
case 'b':
{
int ch;
ch=0;
cout << "Select Team: " <<"1 for " << t1.nameT << ", 2 for " << t2.nameT << endl;
cin >> ch;
for (int i=10; i>0; i--)
{
if ((ch!=1) && (ch!=2))
{
cout << "Please press only 1 or 2 from keyboard " << i << " tries left.." << endl;
cin >> ch;
}
}
switch(ch)
{
case 1:
{
t1.status();
break;
}
case 2:
{
t2.status();
break;
}
}
break;
}
case 'c':
{
int ch2;
ch2=0;
cout << "Select by name(Press 1) or by position in the table(Press 2)" << endl;
cin >> ch2;
for (int i=10; i>0; i--)
{
if ((ch2!=1) && (ch2!=2))
{
cout << "Please press only 1 or 2 from keyboard " << i << " tries left.." << endl;
cin >> ch2;
}
}
switch(ch2)
{
case 1:
{ cout << "Select by name: " << endl;
int ch;
ch=0;
cout << "Select Team: " <<"Press 1 for " << t1.nameT << " or Press 2 for " << t2.nameT << endl;
cin >> ch;
for (int i=10; i>0; i--)
{
if ((ch!=1) && (ch!=2))
{
cout << "Please press only 1 or 2 from keyboard " << i << " tries left.." << endl;
cin >> ch;
}
}
switch(ch)
{
case 1:
{
int b;
cout << "Names of Players in Team " << t1.nameT << endl;
for(int i=0; i>t1.numberofplayers; i++)
{
cout << "Press "<< i << " for " << t1.table[i].nameP << endl;
}
cin >> b;
t1.table[b].status();
}
case 2:
{
int ak;
cout << "Names of Players in Team " << t2.nameT << endl;
for(int i=0; i>t2.numberofplayers; i++)
{
cout << "Press "<< i << " for " << t2.table[i].nameP << endl;
}
cin >> ak;
t2.table[ak].status();
}
}
break;
}
case 2:
{
int ch1;
ch1=0;
cout << "Select Team: " <<"Press 1 for " << t1.nameT << " or Press 2 for " << t2.nameT << endl;
cin >> ch1;
for (int i=10; i>0; i--)
{
if ((ch1!=1) && (ch1!=2))
{
cout << "Please press only 1 or 2 from keyboard " << i << " tries left.." << endl;
cin >> ch1;
}
}
switch(ch1)
{
case 1:
{
int b1;
cout << "Select by table position: " << endl;
cout << "Table with names of players" << endl <<" [form of table is (number of position) -> player name ] " <<endl;
for(int i=0; i>t1.numberofplayers; i++)
{
cout << "( "<< i << " ) --> " << t1.table[i].nameP << endl;
}
cin >> b1;
t1.table[b1].status();
break;
}
case 2:
{
int ak1;
cout << "Select by table position: " << endl;
cout << "Table with names of players" << endl <<" [form of table is (number of position) -> player name ] " <<endl;
for(int i=0; i>t2.numberofplayers; i++)
{
cout << "( "<< i << " ) --> " << t2.table[i].nameP << endl;
}
cin >> ak1;
t2.table[ak1].status();
break;
}
}
break;
}
}
break;
}
case 'd':{
cout<<"The Program is terminated"<<endl;
return 0;
}
}
return 0;
}
与我制作的那些书柜
Player.h
:
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Player
{
public:
string nameP, sex, job, lucky_object;
int age, votes, publicvotes;
int power, hunger;
Player();
Player(string nm, string se, string jb, string lucky_obj, int ag, int vts, int pvts, int pwer, int hnger);
~Player();
void setName(string value);
void setSex(string value);
void setJob(string value);
void setLucky_obj(string value);
void setAge(int value);
void setVotes(int value);
void setPublicvotes(int value);
void setPower(int value);
void setHunger(int value);
string getName();
string getSex();
string getJob();
string getLucky_obj();
int getAge();
int getVotes();
int getPublicVotes();
int getPower();
int getHunger();
int random(int min, int max);
void status();
void work();
void eat();
void sleep();
void playing();
};
#endif
Player.cpp
:
#include "Player.h"
#include "Team.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//Function for random numbers
int Player::random(int min, int max)
{
float r = (int)rand() / (float)RAND_MAX;
return min + r * (max - min);
}
Player::Player()
{
nameP = "";
sex = "";
job = "";
lucky_object = "";
age = 0;
votes = 0;
publicvotes = 0;
power = 100;
hunger = 0;
}
Player::Player(string nmP, string se, string jb, string lucky_obj, int ag, int vts, int pvts, int pwer, int hnger)
{
nameP = nmP;
sex = se;
job = jb;
lucky_object = lucky_obj;
age = ag;
votes = vts;
publicvotes = pvts;
power = pwer;
hunger = hnger;
}
Player::~Player()
{
cout << "Player object with name " << nameP << "is destroyed" << endl;
}
void Player::setName(string value)
{
nameP = value;
}
void Player::setSex(string value)
{
sex = value;
}
void Player::setJob(string value)
{
job = value;
}
void Player::setLucky_obj(string value)
{
lucky_object = value;
}
void Player::setAge(int value)
{
age = value;
}
void Player::setVotes(int value)
{
votes = value;
}
void Player::setPublicvotes(int value)
{
publicvotes = value;
}
void Player::setPower(int value)
{
power = value;
}
void Player::setHunger(int value)
{
hunger = value;
}
//all the getters for the data
string Player::getName()
{
return nameP;
}
string Player::getSex()
{
return sex;
}
string Player::getJob()
{
return job;
}
string Player::getLucky_obj()
{
return lucky_object;
}
int Player::getAge()
{
return age;
}
int Player::getVotes()
{
return power;
}
int Player::getPublicVotes()
{
return publicvotes;
}
int Player::getPower()
{
return power;
}
int Player::getHunger()
{
return hunger;
}
void Player::status()
{
cout << "OBJECT : Player" << endl;
cout << "Name: "<< nameP << endl << "Sex: " << sex << endl;
cout << "Job: "<< job << endl << "Lucky Object: "<< lucky_object << endl;
cout << "Age: "<< age << endl << "Power: " << power << "%" << endl;
cout << "Hunger: "<< hunger << "%" << endl;
cout << "Votes: "<< votes << endl << "Public Votes: " << publicvotes << endl;
cout << endl << endl;
}
void Player::work()
{
power = power - (random(30, 60)*power);
hunger = hunger + ((20/100)*hunger);
}
void Player::eat()
{
power = power + random(10, 40);
hunger += 80;
if (hunger>100)
hunger = 100;
if (power>100)
power = 100;
}
void Player::sleep()
{
power = 100;
}
void Player::playing()
{
power = power - (random(20, 50)*power);
hunger += 25;
if (hunger>100)
hunger = 100;
}
Team.h
:
#ifndef TEAM_H_INCLUDED
#define TEAM_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <string>
#include "Player.h"
using namespace std;
class Team
{
public:
string nameT;
int wins, supplies, numberofplayers;
Player table[10];
Team();
Team(string nmT, int ws, int numberofpl, int supp);
~Team();
void setName(string value);
void setWins(int value);
void setNumberofplayers(int value);
void setSupplies(int value);
string getName();
int getWins();
int getNumberofplayers();
int getSupplies();
void status();
void minousupp();
void addplayer();
};
#endif
Team.cpp
:
#include "Team.h"
#include "Player.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
// Starting and Final condition functions
Team::Team()
{
nameT = "";
wins = 0;
numberofplayers = 0;
supplies = 300;
}
Team::Team(string nmT, int ws, int numberofpl, int supp)
{
nameT = nmT;
wins = ws;
numberofplayers = numberofpl;
supplies = supp;
}
Team::~Team()
{
cout << "Team object with name " << nameT << "is destroyed" << endl;
}
//all the setters for the data
void Team::setName(string value)
{
nameT = value;
}
void Team::setWins(int value)
{
wins = value;
}
void Team::setNumberofplayers(int value)
{
numberofplayers = value;
}
void Team::setSupplies(int value)
{
supplies = value;
}
//all the getters for the data
string Team::getName()
{
return nameT;
}
int Team::getWins()
{
return wins;
}
int Team::getSupplies()
{
return supplies;
}
int Team::getNumberofplayers()
{
return numberofplayers;
}
//Status (returns the data of the object)
void Team::status()
{
cout << "OBJECT: Team" << endl << "Name: " << nameT << endl;
cout << "Players: ";
for (int i=0; i>=numberofplayers; i++)
{
cout << table[i].getName()<<" ,";
}
cout <<endl;
cout << "Wins: " << wins << endl;
cout << "Supplies: " << supplies << endl << "Number of players: "<< numberofplayers << endl;
}
void Team::addplayer()
{
string s1, s2, s3, s4;
int num1, num2, num3;
Player new1;
cout << "Give Player's name: " << endl;
cin >> s1;
cout << "Give Player's sex: " << endl;
cin >> s2;
cout << "Give Player's Job: " << endl;
cin >> s3;
cout << "Give Player's lucky object: " << endl;
cin >> s4;
cout << "Give Player's age:(int only) " << endl;
cin >> num1;
cout << "Give Player's votes:(int only) " <<endl;
cin >> num2;
cout << "Give Player's public votes:(int only) " << endl;
cin >> num3;
new1(s1, s2, s3, s4, num1, num2, num3, 100, 0);
numberofplayers++;.
table[numberofplayers] = new1;
}