c++ - 我收到错误 "invalid use of incomplete type ' 类映射'

如果我的问题很愚蠢,我正在制作一个基于文本的基本 RPG,因为我是 C++ 新手。所以基本上我有一个小型战斗类,我必须从 map 类来回链接,所以我不得不转发声明我的 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;
    }
};

最佳答案

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

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

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

关于c++ - 我收到错误 "invalid use of incomplete type ' 类映射',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013901/

相关文章:

c++ - VS2012 在 64 位目标中 vector 的性能不佳

c++ - 如何强制 gcc 链接未使用的静态库

c++ - 计算机如何进行浮点运算?

c++ - (为什么)移动构造函数或移动赋值运算符应该清除它的参数吗?

c++ - 持有派生类引用的基类的 std::unique_ptr 在 gcc 编译器中不显示警告,

c++ - 如何让我的类(class)成为 google-test 类(class)的 friend

c++ - 动态和静态范围程序差异

c++ - C 和 C++ 中的 1LL 或 2LL 是什么?

c++ - 如何设置 QMainWindow 标题

c++ - 最快的素数测试算法