C++ Class 属性 无法被其他 class 识别(headers 包括,public)
C++ Class property not recognised by other class (headers incl., public)
我在 Header 文件中有两个 class,每个文件分别名为 "Map" 和 "Character"。 "Map" 有一个名为 "map" 的 class,"Character" 有一个名为 "character" 的 class。
Class "map" 继承了 class "character",并且都包含了对方的 header 文件。
在 class "map" 的一种方法中,我使用了 class "character" 中的 属性 成员,并且工作正常。但是,当我尝试在 class "character" 中使用 "map" 属性 成员时,它不起作用。无论 "map" 是否继承 "character",都会发生这种情况。
这是 class 地图,工作正常:
#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";
}
}
这是 class 字符,编译时出现以下错误:
- 'map':不是 class 或命名空间名称
- 'map':不是 class 或命名空间名称
- '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;
}
}
Map.h
包括 Character.h
和 vice-versa,但这不起作用(如果不是 #pragma once
,它将创建无限包含递归) .
因为character
不能依赖map
(因为map
是character
的派生class),所以不应该包括Map.h
.
我对改进代码库的建议:
- 移除
using namespace std;
.
map
已经是 std
命名空间中的 class。最好使用不同的名称或在您自己的命名空间下使用它。
namespace MyApp
{
class character { ... };
}
namespace MyApp
{
class map : public character { ... };
}
我在 Header 文件中有两个 class,每个文件分别名为 "Map" 和 "Character"。 "Map" 有一个名为 "map" 的 class,"Character" 有一个名为 "character" 的 class。 Class "map" 继承了 class "character",并且都包含了对方的 header 文件。 在 class "map" 的一种方法中,我使用了 class "character" 中的 属性 成员,并且工作正常。但是,当我尝试在 class "character" 中使用 "map" 属性 成员时,它不起作用。无论 "map" 是否继承 "character",都会发生这种情况。
这是 class 地图,工作正常:
#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";
}
}
这是 class 字符,编译时出现以下错误:
- 'map':不是 class 或命名空间名称
- 'map':不是 class 或命名空间名称
- '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; } }
Map.h
包括 Character.h
和 vice-versa,但这不起作用(如果不是 #pragma once
,它将创建无限包含递归) .
因为character
不能依赖map
(因为map
是character
的派生class),所以不应该包括Map.h
.
我对改进代码库的建议:
- 移除
using namespace std;
. map
已经是std
命名空间中的 class。最好使用不同的名称或在您自己的命名空间下使用它。
namespace MyApp
{
class character { ... };
}
namespace MyApp
{
class map : public character { ... };
}