无法访问获取错误变量
Getting error variable is inaccessible
我一直在为一款基于主机的小型游戏编写一些代码,该游戏以玩家和一些敌人为特色。到目前为止,我只为我的播放器类编写了一个方法。玩家类是一个名为实体的父类的子类,该实体也有一个称为敌人的子类。在player.cpp的方法void player::takeInput()
中,我尝试修改值this->xCoordinate
,其中int xCoordinate
是实体类的公共成员。每当我编译这个代码时,我都会收到错误:
includeentity.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行出现错误,错误为:"error within This context"。
我认为这个错误与类如何相互继承有关。我能找到的唯一解决方案是解决一个成员被贴上私人标签的问题,这不适用于这个问题,因为我把我的成员贴上了公共标签。
我不是一个经验丰富的程序员,我只会把自己评为中级程序员,所以如果代码包含多个错误,请原谅!
提前感谢您的帮助!这是源代码,请注意,我还没有在项目的其余部分做太多工作:
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不包含代码,因为它没有方法。它只被用作敌人.h和玩家.h 的分组
player.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还没有代码。我使用的是带有代码块的GNU编译器谢谢
您的enemy
类对entity
使用默认(私有)继承,因此它继承的所有entity
字段(包括xCoordinate
)都是私有的,对程序中的任何其他字段都不可见。在敌人.h,你想改变:
class enemy : entity
{
// ...
}
至:
class enemy : public entity
{
// ...
}
这就是你(正确地)在player.h中为player
类设置它的方式。
以下是C++常见问题解答中的一页,其中包含有关私有继承的更多详细信息:https://isocpp.org/wiki/faq/private-inheritance.
- 提升错误:变量"TimeSpec RQTP"具有初始值设定项,但类型不完整
- C++ SPDLOG 编译错误:变量或字段"set_error_handler"声明为无效
- 错误 - 变量类型 "X" 是一个抽象类 - c++
- 带有 std::map 的模板函数给出错误:变量或字段声明为 void
- C 错误变量大小对象可能不会初始化
- 我收到错误"变量类型不完整"无效,我找不到问题所在
- C++以友元身份重载 ostream 运算符会导致错误:变量在此上下文中是私有的
- 错误:变量未在此范围内声明
- 错误:变量未在此范围 C++ 中声明
- 无法访问获取错误变量
- C++错误"变量或字段声明为无效"
- 错误:变量需要堆栈帧
- VS2010 中 C++ lambda 表达式的奇怪错误(变量 y1 )
- 我不断收到错误"变量'b'未初始化正在使用,我不确定如何解决它
- 如何更正运行时错误变量周围的堆栈已损坏
- 错误变量受保护
- 线程池的 lambda 函数内的编译器错误变量"未捕获"
- 接收错误变量的类型不完整"void"
- 运行时错误-变量arr_processes周围的堆栈已损坏
- 错误:变量未在此范围内声明