Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试将新类型合并到现有类型时出现错误C2079和错误C2440_C++ - Fatal编程技术网

C++ 尝试将新类型合并到现有类型时出现错误C2079和错误C2440

C++ 尝试将新类型合并到现有类型时出现错误C2079和错误C2440,c++,C++,我不确定为什么会出现这些错误,我认为这是因为我需要一个转发声明,但问题仍然存在。我不知道是什么原因造成的。我把评论放在冒犯的地方。寻找原因的解释和解决方案。谢谢 // Player.h #ifndef PLAYER_H #define PLAYER_H #include "Weapon.h" #include "Monster.h" #include <string> // Forward declaration class Race; class Player { publ

我不确定为什么会出现这些错误,我认为这是因为我需要一个转发声明,但问题仍然存在。我不知道是什么原因造成的。我把评论放在冒犯的地方。寻找原因的解释和解决方案。谢谢

// Player.h

#ifndef PLAYER_H
#define PLAYER_H

#include "Weapon.h"
#include "Monster.h"
#include <string>

// Forward declaration
class Race;

class Player
{
public:
    Player();

/*  Return Type     Method Name */
    bool            IsDead();
    std::string     GetName();
    int             GetArmor();

    void            TakeDamage(int damage);
    void            CreateClass();
    bool            Attack(Monster& monster);
    void            LevelUp();
    void            Rest();
    void            ViewStats();
    void            Victory(int xp, Monster* monster);
    void            GameOver();
    void            DisplayHitPoints();
    void            SetRace();

private:
/*  Type                Name */
    std::string         m_Name;
    std::string         m_ClassName;
    int                 m_Accuracy;
    int                 m_HitPoints;
    int                 m_MaxHitPoints;
    int                 m_ExpPoints;
    int                 m_NextLevelExp;
    int                 m_Level;
    int                 m_Armor;
    int                 m_Gold;
    Weapon              m_Weapon;
    Race                m_Race;  // problem: error C2079 uses undefined class 'Race'

};

#endif // PLAYER_H
种族类别定义:

// Race.h

#ifndef RACE_H
#define RACE_H

#include <string>

class Race
{
public:
    Race();

/*  Return Type     Method Name*/
    Race            SelectRace();
protected:

    std::string GetName();

/*  Type            Name*/
    std::string     m_Name;
    int             m_Accuracy;
    int             m_HitPoints;

};

// ...below here implements multiple derived classes of type race

#endif // RACE_H
//Race.h
#ifndef种族H
#定义种族
#包括
阶级竞赛
{
公众:
种族();
/*返回类型方法名*/
种族选择种族();
受保护的:
std::string GetName();
/*类型名*/
std::字符串m_名称;
内模精度;
int m_生命点;
};
//…下面实现了race类型的多个派生类
#endif//RACE_H

是的,正如您所发现的,转发声明在这里是不够的。这是因为在
Player
中有一个
Race
类型的成员

在播放器顶部包括整个标题。h:

#include "Weapon.h"
#include "Monster.h"
#include "Race.h"

然后你的
Race
类的定义将对
Player

@igortandtnik的定义可见,它们在违规行旁边的注释中。你需要在Player的顶部包含Race。为了发生问题,程序需要包含所有这些行吗?删除某些函数和成员时发生了什么?要创建一个最小的测试用例?@Borgeader:请不要将答案写为注释。谢谢。@LightnessRacesinOrbit我会继续这样做,只是因为你让我不要这么做。是的,就是这样……奇怪的是,导致我尝试使用转发声明的原因是,我最初使用的是这样的声明,它抛出了一个错误,抱怨同样的事情,但我忘记了Race.cpp文件中的默认构造函数定义。谢谢。
#include "Weapon.h"
#include "Monster.h"
#include "Race.h"