Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

c++ - I'm getting an error "invalid use of incomplete type 'class map'

I am making a basic text based RPG sorry if my question is stupid because im new to c++. So basically I have a small combat class that i have to link back and forth to from the map class so I had to forward declare my map class and it comes up with the error. BTW sorry there aren't any comments.

Here is the error: 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
";
        cout <<"What do you do?
";
        cout <<"---------------------------
";
        cout <<"|-------------------------|
";
        cout <<"|----Attack-----Defend----|
";
        cout <<"|-------------------------|
";
        cout <<"---------------------------
";
        cin >>cchoice;
        this->battle();
    }
    void newturn()
    {
        cout <<"The orc is still alive!";
        cout <<"What do you do?";
        cout <<"
---------------------------
";
        cout <<"|-------------------------|
";
        cout <<"|----Attack-----Defend----|
";
        cout <<"|-------------------------|
";
        cout <<"---------------------------
";
        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.
";
            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
";
                    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
";
                cout <<damagedealt<<" points of damage your health is
";
                cout <<playerobj.health;
                if (playerobj.health>0)
                {
                    this->newturn();
                }
                else if (playerobj.health<=0)
                {
                    cout <<playerobj.name<<"was killed
";
                    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 enemey";
             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
";
        cout <<"mouth of a mighty river to the north
";
        cout <<"What do you want to do?
";


    }


    void relaypointb()
    {
    playerobj.location=="relaypointb";
    cout << "

%%%%%%%%%%%%%%%%%%%%
";
    cout << "%                  %
";
    cout << "%   #Wild North#   %
";
    cout << "%                  %
";
    cout << "%%%%%%%%%%%%%%%%%%%%

";
    cout <<"You have entered the wild north this is where your journey starts
";
    cout <<"What would you like to do

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

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

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

        }
        else if (mchoice=="West")
        {
            this->relaypointaespawn();
        }
        else
        {
            cout <<"Invalid command

";
            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;
    }
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your first usage of Map is inside a function in the combat class. That happens before Map is defined, hence the error.

A forward declaration only says that a particular class will be defined later, so it's ok to reference it or have pointers to objects, etc. However a forward declaration does not say what members a class has, so as far as the compiler is concerned you can't use any of them until Map is fully declared.

The solution is to follow the C++ pattern of the class declaration in a .h file and the function bodies in a .cpp. That way all the declarations appear before the first definitions, and the compiler knows what it's working with.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...