singleton in c++ compile error [duplicate] - c++

This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 7 years ago.
I tried to make the following singleton class in C++
#pragma once
#include "stdafx.h"
#include <iostream>
class God
{
private:
static God* firstObject;
God()
{
if (firstObject != NULL)
firstObject = this;
}
public:
static God* BuildGod()
{
if (firstObject != NULL)
return firstObject;
else {
God();
return firstObject;
}
}
};
and then use it like so
God* a = God::BuildGod();
Unfortunatley, it won't even compile and it returned the following errors:
LNK2001 unresolved external symbol "private: static class God * God::firstObject" (?firstObject#God##0PAV1#A)
LNK1120 1 unresolved externals

You actually have a worse problem than the build error, because you create a temporary object, and save a pointer to it to be used.
The statement
God();
creates a temporary object, and in the constructor you save a pointer to this temporary object in firstObject which you then return. Using that pointer will then lead to undefined behavior.
One of the usual ways of creating a singleton in C++ would be something like this:
class God
{
public:
static God& get_instance() {
{
static God instance; // This is THE instance
return instance;
}
private:
God() {}
};

A static member of a class has to be defined outside the class, like
class God
{
...
};
God* God::firstObj;
Just beware that having the definition in a header file and including it mamy times calls for trouble.

Related

Unresolved External Symbol calling inherited class function [duplicate]

This question already has answers here:
C++ inlining class methods causes undefined reference
(3 answers)
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 2 years ago.
I'm trying to have a class call a function from an inherited class however whenever I do I get an unresolved external error LNK2019. I'm confused to why this is happening as all of the files appear to be properly included, I haven't overridden the function & Visual Studio doesn't complain about it prior to compiling.
I've tried:
Running the replacing the function call with the actual code, this fixes one of the two errors but I still get an unresolved external as the functions in question are calling another function from another class.
Messing around with namespaces, as the inherited class is under a separate namespace I thought that was the problem e.g. Memory::Allocate(...).
Directly calling the class function, so instead of Allocate(...) I tried Memory::GlobalMemoryUser::Allocate(...)
Calling the function using this->...
Relevant code from GlobalMemoryUser.h
#include "MemoryManager"
namespace Memory
{
namespace Internal
{
extern MemoryManager* GlobalMemoryManager;
{
class GlobalMemoryUser
{
public:
// Allocator and deallocator
inline const void* Allocate(size_t, const char* user);
inline const void DeAllocate(void*);
private:
// A pointer to the global memory manager
Internal::MemoryManager* GlobalMemoryManager;
};
}
Relevant code from GlobalMemoryManager.cpp
#include "GlobalMemoryUser.h"
namespace Memory {
const void* GlobalMemoryUser::Allocate(size_t size, const char* user)
{
return GlobalMemoryManager->Allocate(size, user);
}
const void GlobalMemoryUser::DeAllocate(void* p)
{
GlobalMemoryManager->DeAllocate(p);
}
}
Relevant code from System Manager.h
#include "GlobalMemoryUser.h"
namespace ECS
{
class SystemManager : public Memory::GlobalMemoryUser
{
// Constructor and Deconstructor
SystemManager();
~SystemManager();
};
}
Relevant code from SystemManager.cpp
#include "SystemManager.h"
namespace ECS
{
SystemManager::SystemManager()
{
systemAllocator = new Memory::LinearAllocator(ECS_SYSTEM_MEMORYBUFFERSIZE, Allocate(ECS_SYSTEM_MEMORYBUFFERSIZE, "SystemManager"));
}
SystemManager::~SystemManager()
{
for (std::vector<ISystem*>::reverse_iterator it = systemWorkOrder.rbegin(); it != systemWorkOrder.rend(); it++)
{
(*it)->~ISystem();
*it = nullptr;
}
systemWorkOrder.clear();
systemsTable.clear();
DeAllocate((void*)systemAllocator->GetUsedMemoryStart());
delete systemAllocator;
systemAllocator = nullptr;
}
}
The errors in question happen within the constructor and deconstructor for the system manager when trying to call the allocator and deallocator from the GlobalMemoryUser parent class.
Exact Error Message:
LNK2019 unresolved external symbol "public: void const * __cdecl Memory::GlobalMemoryUser::Allocate(unsigned __int64,char const *)" (?Allocate#GlobalMemoryUser#Memory##QEAAPEBX_KPEBD#Z) referenced in function "public: __cdecl ECS::SystemManager::SystemManager(void)" (??0SystemManager#ECS##QEAA#XZ)

How do I run a member function on a thread? [duplicate]

This question already has answers here:
Start thread with member function
(5 answers)
Closed 3 years ago.
When I compile the code below I get the following error. help me...!
Errors:
Error C2276 '&': illegal operation on bound member function expression
Error C3867 'CCore::Run': non - standard syntax; use '&' to create a pointer to member
I'm not sure if the program gives me an error.
I want to run the "Run" function of the Core class.
Core.cpp file contains only the functions created by the compiler.
I'm learning English so I'm not good yet. So please understand that the whole code.
// main.cpp
#include "Core.h"
#include <thread>
int main()
{
// The conditions below have been success
if (CCore::GetInstance().Init())
{
// The code below fails to compile.
// Error C3867 'CCore::Run': non - standard syntax; use '&' to create a pointer to member
thread main_thread(CCore::GetInstance().Run);
// Error C2276 '&': illegal operation on bound member function expression
thread main_thread(&CCore::GetInstance().Run);
main_thread.join();
}
return 0;
}
// Core.h
#pragma once
#include "Singleton.h"
#include <iostream>
using namespace std;
class CCore : public Singleton<CCore>
{
public:
CCore();
~CCore();
bool Init();
void Run();
};
// Singleton.h
#pragma once
template<typename T>
class Singleton
{
protected:
Singleton()=default;
~Singleton()=default;
public:
static T& GetInstance()
{
static T instance;
return instance;
}
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton) = delete;
};
The compiler tells you the problem: Run() isn't a free function. It's a method, i.e. a function bound to an instance of an object. You have several options, but generally you either let the compiler synthesize a runnable for you, or write a free function yourself:
Let the compiler do all the work: guess what, it can make singletons for you so how cool is that?!
std::thread main_thread([]{
static CCore myCore;
myCore.Run();
});
You want to access that core? Sure!
std::future<CCore*> coreWhenDone = std::async([]{
static CCore myCore;
myCore.Run();
return &myCore;
});
Better yet, the core would provide some result, so that instead of accessing it directly when it's done, you could get its result (e.g. an int or std::vector<double> or whatever that core is computing)/
Let the compiler do some of the work:
std::thread main_thread([]{ CCore::GetInstance().Run(); });
Split the work between yourself and the compiler:
std::thread main_thread(std::bind(&CCore::Run, &CCore::GetInstance()));
Do all the work yourself:
void runCoreRun() {
CCore::GetInstance().Run();
}
...
std::thread main_thread(&runCoreRun);

Why can't static member methods access its non-static method in C++? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I had to make a callback interface for a new module while I work, so I made a static method in a class.
One thing I still don't understand is why I can't call a non-static member method in a static member like this :
class CAdapterUser
{
public:
CAdapterUser() {}
virtual ~CAdapterUser() {}
void Test();
void Test2();
protected:
CAdapter m_Adapter;
unsigned char buffer[16];
static void TestFunc(void* apContext);
};
void
CAdapterUser::TestFunc( void* apContext )
{
// CAdapterUser* pUser = (CAdapterUser*)apContext;
CAdapterUser* pUser = reinterpret_cast<CAdapterUser*>(apContext);
pUser->Test2(); // Compile error : LNK2019
pUser->buffer[0] = 1; // Even though I can access protected member variable?
}
Could someone answer my question?
LNK2019 is unresolved symbol, probably you simply forgot to implement Test2().

Access static variable from static function [duplicate]

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 10 years ago.
It is very important that my function is static, I need to access and modify another static/non-static class member in order to print it out later. How can I do that?
Flow
Class is initiated
Constructor sets variable to something using internal function that must be static
Some time later I print that variable
Example code
#include <iostream>
class MyClass
{
public:
static int s;
static void set()
{
MyClass::s = 5;
}
int get()
{
return MyClass::s;
}
MyClass()
{
this->set();
}
};
void main()
{
auto a = new MyClass();
a->set(); // Error
std::cout << a->get() << std::endl; // Error
system("pause");
}
Error
LNK2001: unresolved external symbol "public: static int MyClass::s" (?s#MyClass##2HA)
LNK1120: 1 unresolved externals
You have declared your static variable, but you have not defined it.
Non-static member variables are created and destroyed as the containing object is created and destroyed.
Static members, however, need to be created independently of object creation.
Add this code to create the int MyClass::s:
int MyClass::s;
Addendum:
C++17 adds inline variables, allowing you code to work with a smaller change:
static inline int s; // You can also assign it an initial value here
^^^^^^

Singleton pattern c++ error

Hi I have implemented a singleton pattern in c++ with the VS2010 and the compiler throw me an error !!
#ifndef __EgEngine__
#define __EgEngine__ 1
#include <esUtil.h>
#include <stdlib.h>
#include <EgGpuManager.h>
class EgEngine
{
public:
EgEngine();
static EgGpuManager GetGpuManager();
~EgEngine();
void EgInit();
private:
EgEngine(const EgEngine &other){};
EgEngine* operator = (const EgEngine &other)const {};
static EgGpuManager GpuManager; // Return this !!
ESContext esContext;
};
#endif
The other class
#ifndef __EgGpuManager__
#define __EgGpuManager__ 1
#include <EgBuffer.h>
#include <EgProgram.h>
class EgGpuManager
{
public:
EgBuffer* GetBuffer();
EgProgram* GetNewProgram();
private:
EgGpuManager();
~EgGpuManager();
EgBuffer buffer;
};
#endif
And when I try to compile I have this error:
1>EgEngine.obj : error LNK2001:
unresolved external symbol "private: static class
EgGpuManager EgEngine::GpuManager" (?GpuManager#EgEngine##0VEgGpuManager##A)
Help me please and thanks.
static EgGpuManager GpuManager; // Return this !! this guy has to be instantiated somewhere in your C++ code.
static class members have to appear in the global scope, so in the C++ file add:
EgGpuManager EgEngine::GpuManager.
By the way, you have a private constructor for EgGpuManager class, which will be a problem in this case because it is created by EgEngine. You're not implementing the singleton correctly. Use a static EgGpuManager *EgGpuManager::Get() method to return an instance, and it will instantiate the class on the first call, then you can do it with a private constructor. Otherwise make them friends.
The line "static EgGpuManager GpuManager;" inside the class declaration of EgEngine is only a declaration: You're saying that this object will exist somewhere. The linker complains that it did not find the object anywhere. To solve this, place an instantiation in one of your source files (in global scope):
EgGpuManager EgEngine::GpuManager;
You must put the instantiation of the singleton object either in the global scope, as suggested by other answers, or in the implementation of GetGpuManager() like this :
EgGpuManager& EgEngine::GetGpuManager()
{
static EgGpuManager GpuManager;
return GpuManager;
}
In this case you need to remove the declaration of GpuManager from the class definition. Also note the & to return a reference, as you certainly don't want to return a copy of the object (that would defeat the purpose of making a singleton). The advantage here is that the object will not be created until GetGpuManager() is called for the first time, whereas all statics in global scope are created when the program starts.