<分区>
我在头文件中分别有两个类,分别称为“ map ”和“字符”。 “Map”有一个名为“map”的类,“Character”有一个名为“character”的类。 “map”类继承了“character”类,两者都包含了对方的头文件。 在“map”类的方法之一中,我使用了“character”类的一个属性成员,效果很好。但是,当我尝试在“character”类中使用“map”属性成员时,它不起作用。无论“map”是否继承“character”,都会发生这种情况。
这是工作正常的类映射:
#pragma once
#include <iostream>
#include "Character.h"
#include "AI.h"
using namespace std;
class map: public character
{
public:
static const int mapRow = 30; //-- all maps are the same size
static const int mapColumn = 116;
char marr[mapRow][mapColumn];
map()
{
for (int i = 0; i<mapRow; i++)
{
for (int j = 0; j<mapColumn; j++)
{
marr[i][j] = ' ';//set whole array to blank
}
}
}
void display(void);
void level1(void);
void level2(void);
void level3(void);
void treeBlueprint(int);
};
//displays the map
void map::display(void)
{
//This displays the level
for (int i = 0; i<mapRow; i++)
{
for (int j = 0; j<mapColumn; j++)
{
cout << marr[i][j];
}
cout << "\n";
}
}
这是类字符,编译时会出现以下错误:
- 'map': 不是类名或命名空间名
- 'map': 不是类名或命名空间名
- 'mapColumn': 未声明的标识符
'mapRow': 未声明的标识符
#pragma once #include "Map.h" #include <iostream> using namespace std; class character { int hpPool = 100; int currentHp =hpPool; public: char Tommy ='?'; int position1; int position2; char movement; character() { position1 = 5; position2 = 5; movement = '0'; } void moveUp(void); void moveDown(void); void moveLeft(void); void moveRight(void); void moveFunct(void); }; void character::moveUp(void) { if (position1 != 1) { position1--; } else { //Hitting a wall } } void character::moveDown(void) { if (position1 != (map::mapRow-2)) { position1++; } else { //Hitting a wall } } void character::moveLeft(void) { if (position2 != 1) { position2--; } else { //Hitting a wall } } void character::moveRight(void) { if (position2 != (map::mapColumn-2)) { position2++; } else { //Hitting a wall } } void character::moveFunct(void) { switch (movement) { case 'w': moveUp(); break; case 'a': moveLeft(); break; case 's': moveDown(); break; case 'd': moveRight(); break; } }