我一直试图垄断我必须做的项目。赋值要求我使用多个类并使用继承。我有3个类,player_properties,game_board和运行。主要是跑步。 game_board包含一个初始化游戏板所有内容的结构。 player_properties包含一个结构,它初始化归属于玩家的所有属性。我想继承结构的所有属性到我的正在运行的类,并继续使用它们,并从正在运行的类中进行游戏的所有运行。以下是代码:
using namespace std;
struct player
{
string piece;
int location;
int money;
int lost;
int houses;
int hotels;
};
class player_properties
{
public:
player *players;
int num_players;
void assign();//alloting the requirements to the palyers and to the board
};
void player_properties::assign()
{
//omitted some code to shorten code
//assigning the players properties
cin>>ch;
piecetaken[ch]=1;
players[i].piece=pieces[ch];
players[i].location=0;
players[i].lost=0;
players[i].money=1500;
players[i].hotels=0;
players[i].houses=0;
}
}
using namespace std;
struct board_properties
{
string name;
int name_length;
int board_location;
string player;
int price;
int owner_id;
int houses_owned;
int hotels_owned;
int piece_length;
};
class game_board
{
public:
board_properties *b_properties
void assigngb();//alloting the requirements to the palyers and to the board
};
void game_board::assigngb()//alloting the requirements to the palyers and to the board
{
b_properties=new board_properties[40];
for ( int j=0;j<40;j++)
{
b_properties[j].board_location=j;
b_properties[j].owner_id=10;
b_properties[j].houses_owned=0;
b_properties[j].hotels_owned=0;
b_properties[j].piece_length=0;
}
}
#include "game_board.cpp"
#include "player_properties.cpp"
using namespace std;
class running :public game_board, public player_properties
{
public:
int pos_ch;
int num_players_playing;
void position();// to see the position of the player
};
void running::position()
{
cout<<players[1].piece<<endl; //does not work, core dump since not initialised
//some other running code
}
int main()
{
running rn;
rn.position();
player_properties p;
p.assign();
game_board gb;
gb.assigngb();
}
当我尝试使用类,player_properties和game_board运行时,它们会初始化变量,但是当我回到运行函数时,这些值无法访问。线路支付者[1]给出了一个核心转储,因为它说玩家[1]无法访问且不存在。