Singleton Instantiation Syntax
Dec 6, 2017 at 10:15pm UTC
I am using VS2017 on Win Pro x64. I am having trouble with the proper syntax for instantiating a Singleton in C++.
As defined, the compiler issues the error:
C4700: Uninitialized local variable 'simA' used.
Can anyone help me with the correct syntax in main() for initializing this Singleton and then running the 'runSimulation' method ?
Simulator.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#ifndef SIMULATOR_H
#define SIMULATOR_H
#include <iostream>
class Simulator
{
public :
static Simulator& getInstance()
{
// Instantiated on first use
static Simulator instance; // Guaranteed to be destroyed.
return instance;
}
private :
// Constructor
Simulator()
{
}
// Avoid copies of the singleton might be created
public :
Simulator( Simulator const & ) = delete ; // C++11
void operator =( Simulator const & ) = delete ; // C++11
void runSimulation( int );
};
void Simulator::runSimulation( int moveStep )
{
std::cout << "Singleton Class Simulation runSimulation method at step: " << moveStep << std::endl;
} // end Simulator::runSimulation
#endif // !SIMULATOR_H
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Simulator.h"
int main()
{
Simulator *simA;
simA->runSimulation( 1 ); // ERROR issued on this line
system( "pause" );
return 0;
}
Last edited on Dec 6, 2017 at 10:18pm UTC
Dec 6, 2017 at 11:06pm UTC
The instantiation takes place on line 12 the first time getInstance() is called.
When you want to use the singleton object all you need to do is call getInstance().
Simulator::getInstance().runSimulation(1);
Dec 7, 2017 at 2:39am UTC
Ok, thank you. Though, this did not work.
I now get two (2) linker errors.
Error LNK2005 "public: void __thiscall Simulator::runSimulation(int)" (?runSimulation@Simulator@@QAEXH@Z) already defined in Simulator.obj SingletonStatic001
Error LNK1169 one or more multiply defined symbols found SingletonStatic001
1 2 3 4 5 6 7 8 9 10 11
#include "Simulator.h"
int main()
{
Simulator::getInstance().runSimulation( 1 );
system( "pause" );
return 0;
}
Last edited on Dec 7, 2017 at 2:41am UTC
Dec 7, 2017 at 10:27am UTC
Move the definition of runSimulation to a source file or make it inline.
Dec 7, 2017 at 12:43pm UTC
Move the definition of runSimulation to a source file or make it inline.
If you make it inline you will have a different "singleton" object in each source file that includes the header file. Put getInstance into a source file.
Dec 7, 2017 at 1:28pm UTC
If you make it inline you will have a different "singleton" object in each source file that includes the header file.
This is not true.
Dec 7, 2017 at 3:54pm UTC
Thank-you for correcting me.
Actually, I meant to say that inlining getInstance() was a problem. However, even that statement turns out to be incorrect.
Years ago I ran into problems with a singleton being constructed separately in different compilation units because getInstance() was inlined.
A test I just ran shows that is not a problem any more.
Sorry for injecting confusion.
Topic archived. No new replies allowed.