0

So basically I am trying to create a class and a (static) function to access it, but for some reason the code refuses to compile and throws 2 "unresolved external symbol" errors.

// game.cpp
#include "game.h"

CGame::CGame()
{
    // ...
}

CGame* CGame::getInstance()
{
    if (s_instance == nullptr)
        s_instance = new CGame();

    return s_instance;
}

void CGame::run()
{
    // ...
}

...

// main.cpp
#include "game.h"

int main()
{
    CGame::getInstance()->run();
}

...

#ifndef _GAME_
#define _GAME_

class CGame
{
private:
    static CGame* s_instance;

public:
    static CGame* getInstance();

    void run(); // For testing purposes
    CGame();
};

#endif _GAME_

What is the problem here, how can I possibly create a class I [can] use without declaring it?

HolyRandom
  • 79
  • 1
  • 11
  • What are the actual error messages from your compiler? A brief mention of "unresolved external symbol" errors isn't good enough, especially if we don't know the names of the symbols. – jjramsey Feb 06 '20 at 21:26
  • 1
    You should always provide the text of error messages verbatim. That aside this is probably a duplicate of ["Unresolved external symbol on static class members"](https://stackoverflow.com/questions/195207/unresolved-external-symbol-on-static-class-members). – G.M. Feb 06 '20 at 21:27
  • 3
    Does this answer your question? [Unresolved external symbol on static class members](https://stackoverflow.com/questions/195207/unresolved-external-symbol-on-static-class-members) – anastaciu Feb 06 '20 at 21:34

1 Answers1

1
CGame* CGame::s_instance = nullptr;

is missing in your cpp.

HolyRandom
  • 79
  • 1
  • 11
Totonga
  • 4,236
  • 2
  • 25
  • 31