【发布时间】:2017-01-11 02:29:55
【问题描述】:
我一直在为一个基于控制台的小型游戏编写一些代码,该游戏包含一个玩家和一些敌人。到目前为止,我只为我的播放器类编写了一种方法。
玩家类是称为实体的父类的子类,该父类也有一个称为敌人的子类。在 player.cpp 的方法void player::takeInput() 中,我尝试修改值this->xCoordinate,其中int xCoordinate 是实体类的公共成员。每当我编译这段代码时,都会出现错误:
include\entity.h line 31 error: 'int entity::xCoordinate' is inaccessible
C:\Users....\ line 72 error: 'error within this context'
请注意,第 72 行的代码是:
int newX = this->xCoordinate;
我每次对this->xCoordinate 或this->yCoordinate 进行的所有其他(许多)调用都会重复此错误,错误出现在 entity.h 的第 31 行,并且错误:“在此上下文中出错”。
我认为这个错误与类如何相互继承有关。我能找到的唯一解决方案是解决成员被标记为私有的问题,这不适用于此问题,因为我已将我的成员标记为公开。
我不是一个经验丰富的程序员,只会将自己评为中级,所以如果代码包含多个错误,请原谅!
提前感谢您的帮助! 这是源代码,请注意我还没有对项目的其余部分做太多工作:
main.cpp:
#include <iostream>
#include <stdlib.h>
#include "entity.h"
#include "player.h"
#include "enemy.h"
#include <vector>
using namespace std;
int playerX;
int playerY;
int noEnemies;
void initGrid()
{
return;
}
void dispGrid()
{
return;
}
int verifyMove(int x, int y)
{
return 0;
}
void aiTurn()
{
return;
}
int main()
{
return 0;
}
实体.h:
#ifndef ENTITY_H
#define ENTITY_H
#include <iostream>
#include <vector>
using namespace std;
enum direction
{
NORTH,
SOUTH,
EAST,
WEST
};
enum weaponClass
{
PISTOL,
SWORD,
RIFLE,
SHOTGUN
};
class entity
{
public:
weaponClass currentWeapon;
int health;
int weaponStrength;
int xCoordinate;
int yCoordinate;
string spritePath;
string name;
protected:
private:
};
#endif // ENTITY_H
entity.cpp 不包含代码,因为它没有方法。它仅用作enemy.h 和player.h 的分组
播放器.h:
#ifndef PLAYER_H
#define PLAYER_H
#include "entity.h"
#include <iostream>
class player : public entity
{
public:
direction nextMoveDir;
direction attackDir;
int ammunition;
player();
virtual ~player();
virtual void addToGrid();
virtual void draw();
void takeInput();
void restoreHealth();
void makeMove();
int health;
protected:
private:
};
#endif // PLAYER_H
player.cpp:(抱歉代码量太大!)
#include "player.h"
#include "entity.h"
#include "enemy.h"
#include <iostream>
#include <vector>
using namespace std;
player::player()
{
//ctor
}
player::~player()
{
//dtor
}
vector<enemy*> listOfEntities;
void player::takeInput()
{
cout << "Would you like to move (1), or attack (2)?" << endl;
int input;
cin >> input;
if(input == 1)
{
bool cont = false;
while(!cont)
{
cont = true;
// Moving
cout << "In which direction would you like to move:" << endl;
cout << "1) North, 2) South, 3) East, 4) West." << endl;
int newX = this->xCoordinate;
int newY = this->yCoordinate;
cin >> input;
switch(input)
{
case 1:
this->nextMoveDir = NORTH;
newX++;
break;
case 2:
this->nextMoveDir = SOUTH;
newX--;
break;
case 3:
this->nextMoveDir = EAST;
newY++;
break;
case 4:
this->nextMoveDir = WEST;
newY--;
break;
default:
cont = false;
cout << "Invalid selection." << endl;
break;
}
for(int i = 0; i < listOfEntities.size(); i++)
{
int itrX = listOfEntities[i]->xCoordinate;
int itrY = listOfEntities[i]->yCoordinate;
if(((newX == this->xCoordinate) && (newY = this->yCoordinate))) )
{
// tile is occupied
cout << "That tile is occupied!" << endl;
cont = false;
break;
}
}
}
}
else if(input == 2)
{
// Attacking
bool cont = false;
while(!cont)
{
cont = true;
switch(this->currentWeapon)
{
case SWORD:
cout << "Your current weapon is a sword that has a range of 1 and a damage rating of 5." << endl;
cout << "In which direction do you wish to attack:" << endl;
cout << "1) North, 2) South, 3) East, 4) West." << endl;
int input;
cin >> input;
enemy *enemyToAttack;
switch(input)
{
case 1:
this->attackDir = NORTH;
for(int i = 0; i < listOfEntities.size(); i++)
{
int xCor = listOfEntities[i]->xCoordinate;
int yCor = listOfEntities[i]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate+1))
{
enemyToAttack = listOfEntities[i];
}
}
break;
case 2:
this->attackDir = SOUTH;
for(int i = 0; i < listOfEntities.size(); i++)
{
int xCor = listOfEntities[i]->xCoordinate;
int yCor = listOfEntities[i]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate-1))
{
enemyToAttack = listOfEntities[i];
}
}
break;
case 3:
this->attackDir = EAST;
for(int i = 0; i < listOfEntities.size(); i++)
{
int xCor = listOfEntities[i]->xCoordinate;
int yCor = listOfEntities[i]->yCoordinate;
if((xCor == this->xCoordinate+1) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[i];
}
}
break;
case 4:
this->attackDir = WEST;
for(int i = 0; i < listOfEntities.size(); i++)
{
int xCor = listOfEntities[i]->xCoordinate;
int yCor = listOfEntities[i]->yCoordinate;
if((xCor == this->xCoordinate-1) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[i];
}
}
break;
default:
cont = false;
cout << "Invalid selection." << endl;
break;
}
if(enemyToAttack == NULL)
{
player::takeInput();
}
enemyToAttack->health = health - 5;
cout << "You attack the enemy for 5 damage!" << endl;
cout << "They are now on " << enemyToAttack->health << "HP." << endl;
break;
case PISTOL:
cout << "Your current weapon is a pistol that has a range of 4 and a damage rating of 3 with " << this->ammunition << " bullets remaining." << endl;
cout << "In which direction do you wish to attack:" << endl;
cout << "1) North, 2) South, 3) East, 4) West." << endl;
cin >> input;
switch(input)
{
case 1:
this->attackDir = NORTH;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate+i))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 2:
this->attackDir = SOUTH;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate-i))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 3:
this->attackDir = EAST;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate+i) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 4:
this->attackDir = WEST;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate-i) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
default:
cont = false;
cout << "Invalid selection." << endl;
break;
}
if(enemyToAttack == NULL)
{
cout << "Your attack missed!" << endl;
break;
}
enemyToAttack->health = health - 3;
cout << "You attack the enemy for 3 damage!" << endl;
cout << "They are now on " << enemyToAttack->health << "HP." << endl;
break;
case RIFLE:
cout << "Your current weapon is a rifle that has a range of 10 and a damage rating of 6 with " << this->ammunition << " bullets remaining." << endl;
cout << "In which direction do you wish to attack:" << endl;
cout << "1) North, 2) South, 3) East, 4) West." << endl;
cin >> input;
switch(input)
{
case 1:
this->attackDir = NORTH;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate+1))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 2:
this->attackDir = SOUTH;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate+1))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 3:
this->attackDir = EAST;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate+i) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
// Confirm that there is nothing in the way of the move.se 4:
this->attackDir = WEST;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate-i) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
default:
cont = false;
cout << "Invalid selection." << endl;
break;
}
if(enemyToAttack == NULL)
{
cout << "Your attack missed!" << endl;
break;
}
enemyToAttack->health = health - 6;
cout << "You attack the enemy for 6 damage!" << endl;
cout << "They are now on " << enemyToAttack->health << "HP." << endl;
break;
case SHOTGUN:
cout << "Your current weapon is a shotgun that has a range of 2 and a damage rating of 10 with " << this->ammunition << " cartridges remaining." << endl;
cout << "In which direction do you wish to attack:" << endl;
cout << "1) North, 2) South, 3) East, 4) West." << endl;
cin >> input;
switch(input)
{
case 1:
this->attackDir = NORTH;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate+1))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 2:
this->attackDir = SOUTH;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate) && (xCor = this->yCoordinate-1))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 3:
this->attackDir = EAST;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinate+1) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
case 4:
this->attackDir = WEST;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < listOfEntities.size(); j++)
{
int xCor = listOfEntities[j]->xCoordinate;
int yCor = listOfEntities[j]->yCoordinate;
if((xCor == this->xCoordinat-1) && (xCor = this->yCoordinate))
{
enemyToAttack = listOfEntities[j];
}
}
break;
}
cout << "Your attack missed!" << endl;
break;
default:
cont = false;
cout << "Invalid selection." << endl;
break;
}
if(enemyToAttack == NULL)
{
cout << "Your attack missed!" << endl;
break;
}
enemyToAttack->health = health - 10;
cout << "You attack the enemy for 5 damage!" << endl;
cout << "They are now on " << enemyToAttack->health << "HP." << endl;
break;
}
}
}
}
敌人.h:
#ifndef ENEMY_H
#define ENEMY_H
#include "entity.h"
#include <iostream>
class enemy : entity
{
public:
enemy();
virtual ~enemy();
void attackPlayer();
void upgradeHealth();
void upgradeWeapons();
int health;
protected:
private:
};
#endif // ENEMY_H
enemy.cpp 还没有代码。 我正在使用带有 Code::Blocks 的 GNU 编译器 谢谢!
【问题讨论】:
-
尝试删除
this->... 不需要 -
class enemy : entity你忘了指定继承的类型。默认为私有。添加public以访问基类的成员
标签: c++ class inheritance