#Unresolved external

40 messages · Page 1 of 1 (latest)

gaunt ermine
#

I'm getting an unresolved external error, but can't figure out why. I'm implementing a singleton according to this site https://refactoring.guru/design-patterns/singleton/cpp/example. My header contains this

class GameLoop {
private:
    static GameLoop* instance;

public:
    GameLoop(GameLoop& other) = delete;
    void operator=(const GameLoop& other) = delete;

    static GameLoop* GetInstance();

And my cpp contains this

#include "GameLoop.h"

GameLoop* GameLoop::instance = nullptr;

GameLoop* GameLoop::GetInstance() {
    if (instance == nullptr)
        instance = new GameLoop();

    return instance;
}

I can't see what the problem with GetInstance is though.

grave zealotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

cloud snow
gaunt ermine
cloud snow
#

oh wait

#

GameLoop is a normal class

#

and second

GameLoop& GameLoop::GetInstance()
{
  static GameLoop instance; // a global variable with thread-safe creation visible only in this scope
  return instance;
}
#

this is a much better way of creating a singleton

gaunt ermine
#

how does that work? To me that looks like you've made a static local variable, which doesn't make sense.

cloud snow
#

a.k.a. static local variable

gaunt ermine
#

That's... this hurts my little C# centric brain.

cloud snow
#

correct

#

C# does not have this

gaunt ermine
#

Ok, well I guess I'll do that then. No idea how that works at all but oh well. Why does what I have not work though?

gaunt ermine
#
error LNK2019: unresolved external symbol "private: __cdecl GameLoop::GameLoop(void)" (??0GameLoop@@AEAA@XZ) referenced in function "public: static class GameLoop * __cdecl GameLoop::GetInstance(void)" (?GetInstance@GameLoop@@SAPEAV1@XZ)```
cloud snow
#

implement it now

gaunt ermine
#

I thought that was implicit.

cloud snow
gaunt ermine
#

Wha-?! Only sometimes?!

cloud snow
#

just implement it

gaunt ermine
#

It works and that's dumb.

cloud snow
#
Deleted implicitly-declared default constructor
#

here

gaunt ermine
#

So if you delete the copy constructor, it will also delete the main constructor. The more you know.

cloud snow
#

yes

gaunt ermine
#

Wait... oh ye I see it now. I did declare it, just down in private where I wasn't looking at it...

cloud snow
#

because, if you explicity decided to delete the copy constructor, the compiler is like
"hmmmmmm, this guy decided that the copy constructor I made is bad, so maybe they are doing some fancy stuff, so to be safe I better NOT generate the default constructor "

gaunt ermine
gaunt ermine
#

High stress work is making me stupid 😩

#

Thank you. Even if the problem was just me blinding my own code, this has been informative.

#

!solved.

#

!solved