我收到一个错误"无效使用不完整类型'类映射' [英] I'm getting an error "invalid use of incomplete type 'class map'
问题描述
如果我的问题很愚蠢,我正在制作基于RPG的基本文本,因为这是C ++的新手。因此,基本上,我有一个小型的战斗类,我必须从地图类来回链接,因此我必须向前声明我的地图类,然后出现错误。顺便说一句,对不起,没有任何评论。
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.
这里是错误:无效使用不完整类型类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 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\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;
}
};
推荐答案
地图的首次使用
在 combat
类的函数内。这是在定义 Map
之前发生的,因此会出现错误。
Your first usage of Map
is inside a function in the combat
class. That happens before Map
is defined, hence the error.
前向声明仅表示将使用特定的类。稍后定义,因此可以引用它或具有指向对象的指针等。但是,前向声明不说明类具有的成员,因此就编译器而言,直到<$ c之前,您不能使用它们中的任何一个。 $ c> Map 已完全声明。
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.
解决方案是在中遵循类声明的C ++模式.h
文件和功能主体放在 .cpp
中。这样,所有声明都会出现在第一个定义之前,并且编译器知道它的工作方式。
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.
这篇关于我收到一个错误"无效使用不完整类型&#39;类映射&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!