gpt4 book ai didi

c++ - 为什么在父类中声明虚函数时出现 Unresolved external 错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:07:46 24 4
gpt4 key购买 nike

我有这个父类:

enum UI_STATE
{
UI_STATE_SPLASH_SCREEN,
UI_STATE_LOGIN_SCREEN,
UI_STATE_CHARACTER_CREATION_SCREEN,
UI_STATE_CHARACTER_CHOOSE_SCREEN,
UI_STATE_LOADING_SCREEN,
UI_STATE_GAMEPLAY,
UI_STATE_EXIT_REQUESTED,
UI_STATE_UNKNOWN
};

[event_source(native)]
class UserInterface
{
protected:
MyGUI::Gui *mGUI;

public:
static UserInterface *Instance;
UI_STATE UI_CURRENT_STATE;

public:
UserInterface()
{
MyGUI::OgrePlatform* mPlatform = new MyGUI::OgrePlatform();
mPlatform->initialise(BaseObjects::mWindow, BaseObjects::mSceneMgr);
mGUI = new MyGUI::Gui();
mGUI->initialise();

UI_CURRENT_STATE = UI_STATE_UNKNOWN;
}

~UserInterface()
{
mGUI->destroyAllChildWidget();
mGUI->shutdown();

delete mGUI;
mGUI = NULL;

delete Instance;
Instance = NULL;
}

virtual void update();
virtual void GAMEPLAY_SCREEN_ShowTargetBox();
virtual void GAMEPLAY_SCREEN_HideTargetBox();

...//some other methods
}

UserInterface *UserInterface::Instance = NULL;

还有两个子类,其中一个是覆盖这 3 个虚函数,第二个对这 3 个函数不做任何事情。

child 1:

#ifndef GameplayScreenInterface_h
#define GameplayScreenInterface_h

#include "UserInterface.h"
#include "ControllableCharacterAdv.h"

class GameplayScreenUserInterface : public UserInterface
{
private:
...

public:
GameplayScreenUserInterface()
{
...
}

void GAMEPLAY_SCREEN_ShowTargetBox()
{
...
}

void GAMEPLAY_SCREEN_HideTargetBox()
{
...
}

void update()
{
UpdateTargetBox();
UpdateCharacterBox();
}

void UpdateCharacterBox()
{
...
}

void UpdateTargetBox()
{
if (...)
{
if (...)
{
...
}
else if (...)
{
...
}
else
{
...
}
}
else
GAMEPLAY_SCREEN_HideTargetBox();
}
};

#endif GameplayScreenInterface_h

和 child 2:

#ifndef LoginScreenInterface_h
#define LoginScreenInterface_h

#include "UserInterface.h"
#include "NetworkManager.h"

class LoginScreenUserInterface : public UserInterface
{
public:
LoginScreenUserInterface()
{
...
}
};

#endif LoginScreenInterface_h

和编译错误:(

Error 9 error LNK1120: 3 unresolved externals
Error 8 error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::GAMEPLAY_SCREEN_HideTargetBox(void)" (GAMEPLAY_SCREEN_HideTargetBox@UserInterface@@UAEXXZ)
Error 7 error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::GAMEPLAY_SCREEN_ShowTargetBox(void)" (GAMEPLAY_SCREEN_ShowTargetBox@UserInterface@@UAEXXZ)
Error 6 error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::update(void)" (?update@UserInterface@@UAEXXZ)

有人知道如何消除这些错误吗?

最佳答案

这些不是编译错误,而是链接错误。您的源代码编译得很好,但您没有在基类中提供三个虚函数的实现。

当你在类声明中提到一个成员函数时,你所做的只是声明这个函数:你告诉编译器函数的名称是什么,它的参数类型是什么,它的返回值是什么类型;这让编译器很高兴。您仍然需要提供一些实现,或将函数标记为抽象,以满足链接器的要求。

在你实现UserInterface的cpp文件中添加这些:

void UserInterface::update() {
// default implementation
}
void UserInterface::GAMEPLAY_SCREEN_ShowTargetBox() {
// default implementation
}
void UserInterface::GAMEPLAY_SCREEN_HideTargetBox() {
// default implementation
}

或者,如果部分或所有这些虚函数没有默认实现,则在 header 中添加 = 0:

virtual void update() = 0;
virtual void GAMEPLAY_SCREEN_ShowTargetBox() = 0;
virtual void GAMEPLAY_SCREEN_HideTargetBox() = 0;

关于c++ - 为什么在父类中声明虚函数时出现 Unresolved external 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452160/

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