gpt4 book ai didi

c++ - 尽管使用了 include,但仍使用未定义的类

转载 作者:行者123 更新时间:2023-12-02 15:21:01 27 4
gpt4 key购买 nike


我想在另一个类中创建我的游戏类的实例,但我得到了一个uses undefined class错误,尽管我包含了game.h

游戏.h

#ifndef GAME_H
#define GAME_H

#include "Move.h"

class Game
{
private:

//--------------------------------------------------------------------------
// Private copy constructor
//
Game(const Game& original);

public:
//--------------------------------------------------------------------------
// Public assignment operator
//
Game& operator=(const Game& original);

//Constructors
Game ();

//Destructor
~Game ();
};
#endif // _GAME_H

我想创建游戏对象的页眉:

移动.h

#ifndef MOVE_H
#define MOVE_H

#include "Game.h"

class Move : public Command
{
private:
Move (const Move& original);
Move& operator=(const Move& original);

Game m_game_; //Error
};
#endif // _MOVE_H

Visual Studios 正在抛出:

    move.h(40): error C2079: 'Move::m_game_' uses undefined class 'Game'

如果我使用 Game* game; 它会起作用,但这不是我需要/想要的。

为什么编译器给我一个未定义类的错误?
非常感谢任何帮助。

最佳答案

Game.h 不需要包含 Move.h,因为它没有被使用,如评论中所述,如果它在 cpp 中使用,它应该包含在那里。

问题可能与来自其他地方的包含头文件的顺序有关,比如 main。考虑这个结构:

(我删除了一些以便更容易看到)

main.cpp 文件

#include "Game.h"
#include "Move.h"

游戏.h文件

#ifndef GAME_H
#define GAME_H

#include "Move.h"

class Game
{
public:
Game() {}
private:

};
#endif // _GAME_H

现在让我们看看 Move.h 文件添加到 Game.h 后的样子

    #ifndef GAME_H
#define GAME_H

//added
#ifndef MOVE_H
#define MOVE_H

#include "Game.h"

class Move
{
public:
Move() {}
private:


Game m_game_;
};
#endif // _MOVE_H

//to here

class Game
{
public:
Game() {}
private:

};
#endif // _GAME_H

如您所见,当它最终声明 Game m_game_ 时;它实际上还没有宣布这个类(class)。要么从包含它们的位置切换包含顺序,要么简单地从 Game.h 中删除 #include "Move.h"因为它没有在那里使用。

关于c++ - 尽管使用了 include,但仍使用未定义的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36795336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com