我一直在为一款基于游戏机的游戏编写一些代码,其中包含玩家和一些敌人。到目前为止,我只为我的播放器类编写了一种方法。
player类是一个名为entity的父类的子类,它也有一个名为enemy的子类。在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;
}
entity.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的分组。
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;
}
}
}
}
enemy.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编译器与Code :: Blocks 谢谢!
答案 0 :(得分:0)
您的tom <- data.frame(a = c(1,2,3), b = c("2017-01-09","2017-01-10","2017-09-11"))
kate <- data.frame(a = c(4,5,6), b = c("2017-01-09","2017-01-10","2017-09-11"))
testList <- list(tom,kate)
f <- lapply(testList, function(x) {
x[,2] <- as.Date(x[,2])
})
类正在使用enemy
的默认(私有)继承,因此它继承的所有entity
字段(包括entity
)都是私有且不可见在你的程序中的任何其他内容。在enemy.h中,您想要更改:
xCoordinate
为:
class enemy : entity
{
// ...
}
这就是你(正确)为player.h中的class enemy : public entity
{
// ...
}
类设置它的方式。
这是C ++ FAQ中的一个页面,其中包含有关私有继承的更多详细信息:https://isocpp.org/wiki/faq/private-inheritance。