私は、プレーヤーといくつかの敵を特徴とする小さなコンソールベースのゲームのためのいくつかのコードを書いてきました。これまでのところ私は自分のプレーヤークラスのメソッドの1つしか書いていません。 プレーヤークラスは、entityという親クラスの子クラスで、敵クラスと呼ばれる子クラスもあります。 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:
enemy.cppにはまだコードがありません。 私はCode :: BlocksでGNUコンパイラを使用しています ありがとう!あなたのenemy
クラスがentity
のデフォルト(プライベート)の継承を使用している
'this->'を削除してみてください...必要ありません –
'class enemy:entity'継承のタイプを指定するのを忘れました。デフォルトはプライベートです。ベースクラスのメンバーにアクセスするために 'public'を追加する –