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.h 包括 Character.h 和 vice-versa,但这不起作用(如果不是 #pragma once,它将创建无限包含递归) .

因为character不能依赖map(因为mapcharacter的派生class),所以不应该包括Map.h.

我对改进代码库的建议:

  1. 移除using namespace std;.
  2. map 已经是 std 命名空间中的 class。最好使用不同的名称或在您自己的命名空间下使用它。

namespace MyApp
{
   class character { ... };
}

namespace MyApp
{
   class map : public character { ... };
}