Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why i getting unresolved externals error when declaring virtual functions in parent class?

I have this parent class:

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;

Also have two child classes, one of those is overrides this 3 virtual functions and second do nothing with this 3 functions.

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

and Child 2:

#ifndef LoginScreenInterface_h
#define LoginScreenInterface_h

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

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

#endif LoginScreenInterface_h

And compilation errors :(

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)

Anyone have any idea how to get rid of that errors?

like image 823
Kosmo零 Avatar asked Dec 12 '22 00:12

Kosmo零


1 Answers

These are not compilation errors, these are link errors. Your sources compiled fine, but you did not supply implementations of the three virtual functions in the base class.

When you mentioned a member function in a class declaration, all you did was declaring the function: you told the compiler what's the name of the function, what are its parameter types, and what's its return type; this makes the compiler happy. You still need to supply some implementation, or mark the functions abstract, in order to satisfy the linker.

In your cpp file with the implementation of UserInterface add these:

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

Alternativelt, if there is no default implementation for some or all of these virtual functions, add = 0 in the header:

virtual void update() = 0;
virtual void GAMEPLAY_SCREEN_ShowTargetBox() = 0;
virtual void GAMEPLAY_SCREEN_HideTargetBox() = 0;
like image 120
Sergey Kalinichenko Avatar answered Jan 19 '23 00:01

Sergey Kalinichenko