我收到错误“无效使用不完整类型‘类映射’

新手上路,请多包涵

如果我的问题很愚蠢,我正在制作一个基于文本的基本 RPG 抱歉,因为我是 C++ 新手。所以基本上我有一个小型战斗类,我必须从地图类来回链接,所以我不得不转发声明我的地图类,它会出现错误。顺便说一句,抱歉没有任何评论。

这是错误: invalid use of incomplete type class Map

 #include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Map;
class Player
{
  public:
    int health;
    int damage;
    int defense;
    int gems=0;
    string race;
    string name;
    string location;
};

class Enemy
{
  public:
    int ehealth;
    int edamage;
    int edefense;
    int echoice;
};

class combat
{
  public:
    Map* mapobj;
    int damagedealt;
    Player playerobj;
    Enemy enemeyobj;
    string cchoice;
    string retry;

    void initial()
    {
        cout <<"A wild orc has appeared\n";
        cout <<"What do you do?\n";
        cout <<"---------------------------\n";
        cout <<"|-------------------------|\n";
        cout <<"|----Attack-----Defend----|\n";
        cout <<"|-------------------------|\n";
        cout <<"---------------------------\n";
        cin >>cchoice;
        this->battle();
    };
    void newturn()
    {
        cout <<"The orc is still alive!";
        cout <<"What do you do?";
        cout <<"\n---------------------------\n";
        cout <<"|-------------------------|\n";
        cout <<"|----Attack-----Defend----|\n";
        cout <<"|-------------------------|\n";
        cout <<"---------------------------\n";
        cin >>cchoice;
        this->battle();
    };
    void battle()
    {
        enemeyobj.echoice = rand() % 2;
        if (enemeyobj.echoice= 1)
        {
            if (cchoice=="Attack")
            {
            playerobj.damage;
            enemeyobj.ehealth=enemeyobj.ehealth-playerobj.damage;
            cout <<"You did "<<playerobj.damage<<" points of damge to the enemey.\n";
            if (enemeyobj.ehealth>0)
            {
                playerobj.health=enemeyobj.edamage-playerobj.health;
                cout <<"The enemyattacked you. You now have "<<playerobj.health<<" health";
                if (playerobj.health>0)
                {
                    this->newturn();
                }
                else if (playerobj.health<=0)
                {
                    cout << playerobj.name << "was killed\n";
                    cout << "Game Over";
                }
            }
            else if (enemeyobj.ehealth<=0)
            {
                 cout <<"You have defeated the orc!";
                 if (playerobj.location=="a")
                 {
                     mapobj->relaypointa();
                 }
            }

            }
            else if (cchoice=="Defend")
            {
                damagedealt=enemeyobj.edamage-playerobj.defense;
                playerobj.health=damagedealt-playerobj.health;
                cout <<"You defend but the enemey was able to deal\n";
                cout <<damagedealt<<" points of damage your health is\n";
                cout <<playerobj.health;
                if (playerobj.health>0)
                {
                    this->newturn();
                }
                else if (playerobj.health<=0)
                {
                    cout <<playerobj.name<<"was killed\n";
                    cout <<"Game Over";
                }
            }
        }
        else if (enemeyobj.echoice=2)
        {
            if (cchoice=="Attack")
            {
                damagedealt=enemeyobj.edefense-playerobj.damage;
                enemeyobj.ehealth=enemeyobj.ehealth-damagedealt;
                cout <<"You did "<<damagedealt<<" points of damage to the enemy";
                if (enemeyobj.ehealth>0)
                {
                    this->newturn();
                }
                else if (enemeyobj.ehealth<=0)
                {
                    cout <<"You have defeated the orc!";
                    mapobj->relaypointa();
                }
            }
            else if (cchoice=="Defend")
            {
                cout <<"Both parties defended";
                this->newturn();
            }
        }
    }
};

class Map
{
    public:
    combat combatobj;
    string mchoice;
    int espawn;
    Player playerobj;
    Enemy enemeyobj;
    void relaypointaespawn()
    {
        playerobj.location=="relaypointa";
        enemeyobj.ehealth = rand() % 50 + 100;
        enemeyobj.edamage = rand() % 50 + 75;
        enemeyobj.edefense = rand() % 50 + 50;
        combatobj.initial();
    }
    void relaypointa()
    {
        cout <<"You have found yourself at the\n";
        cout <<"mouth of a mighty river to the north\n";
        cout <<"What do you want to do?\n";
    }

    void relaypointb()
    {
        playerobj.location=="relaypointb";
        cout << "\n\n%%%%%%%%%%%%%%%%%%%%\n";
        cout << "%                  %\n";
        cout << "%   #Wild North#   %\n";
        cout << "%                  %\n";
        cout << "%%%%%%%%%%%%%%%%%%%%\n\n";
        cout <<"You have entered the wild north this is where your journey starts\n";
        cout <<"What would you like to do\n\n";
        cin >> mchoice;

        if (mchoice=="Travel")
        {
            cout <<"Where would you like to travel?\n";
            cin >>mchoice;
            if (mchoice=="North")
            {

            }
            else if (mchoice=="East")
            {

            }
            else if (mchoice=="South")
            {

            }
            else if (mchoice=="West")
            {
                this->relaypointaespawn();
            }
            else
            {
                cout <<"Invalid command\n\n";
                this->relaypointb();
            }
        }
    }
    void relaypointcespawn()
    {
        playerobj.location=="a";
        enemeyobj.ehealth = rand() % 50 + 100;
        enemeyobj.edamage = rand() % 50 + 75;
        enemeyobj.edefense = rand() % 50 + 50;
        espawn = rand() % 2;
    }
};

原文由 user2998425 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 397
1 个回答

您第一次使用 Map 是在 combat 类的一个函数中。这发生在 Map 被定义之前,因此出现错误。

前向声明仅表示稍后将定义特定类,因此可以引用它或具有指向对象的指针等。但是,前向声明并未说明类具有哪些成员,就编译器而言,您在 Map 完全声明之前,不能使用它们中的任何一个。

解决方案是遵循 .h 文件中的类声明的 C++ 模式和 .cpp 中的函数体。这样,所有声明都出现在第一个定义之前,编译器知道它正在使用什么。

原文由 Adam 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题