C++ Singleton fail to link on Mac OS - c++

I am trying to create a very classic singleton on C++, Mac OS using Xcode
I create the Class MySingleton as follow:
class MySingleton{
private:
int val;
static MySingleton *instance;
MySingleton(){
val = 0;
}
public:
int getVal(){
return val;
}
void setVal(int iVal){
val = iVal;
}
static MySingleton* getInstance(){
if(instance == NULL)
instance = new MySingleton();
return instance;
}
};
Linker is complaining about static MySingleton* getInstance()
Follow the Linker message:
Undefined symbols for architecture x86_64: "MySingleton::instance",
referenced from:
MySingleton::getInstance() in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Could somebody help on this? Perhaps I need to set something on Xcode, which by the way is version 4.2.1, and I am not able in doing this.
Thanks.

You need to define static variable in your cpp file, like this:
MySingleton *MySingleton::instance = 0;

The static member MySingleton needs to be defined in the cpp file. In the header you have have only declared it. See this for more information: Why do static variables need to be declared twice in C++

As far as I can see, you've declared static MySingleton *instance but haven't defined it anywhere.

Related

Cannot access public static variable member [duplicate]

This question already has an answer here:
linker cannot find a C++ static member [duplicate]
(1 answer)
Closed 6 years ago.
I've declared a public static member to keep the total count of the instances of my class. The code is as follows:
class Hello {
public:
static int myCount;
void test(){
//do nothing
};
Hello(){
Hello::myCount += 1;
};
~Hello() {
Hello::myCount -= 1;
}
};
int main(int argc, const char * argv[]) {
// insert code here...
Hello *p1 = new Hello();p1->test();
Hello *p2 = new Hello();p2->test();
cout << Hello::myCount;
return 0;
}
However, I when compile, it says:
Undefined symbols for architecture x86_64:
"Hello::myCount", referenced from:
_main in main.o
Hello::Hello() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't know where I'm wrong. It's been years from the last time I worked with c++, so could you please suggest a solution?
Thank you.
Static members have to be defined outside of the class, e.g.:
class Hello {
public:
static int myCount;
void test(){
//do nothing
};
Hello(){
Hello::myCount += 1;
};
~Hello() {
Hello::myCount -= 1;
}
};
int Hello::myCount = 0; // definition outside of the class
(...)
Here is an example to show, that it helps to solve your problem: http://ideone.com/LVXVCc
It's all because a rule called One Definition Rule.
You can read more about this one in a context of static class members here.
In short: static int myCount declaration is not a definition of a member. Classes are usually placed in their .h/.hpp header files and are included to many other files. If those'd contain static member and lines like the one above would be a definition, it will lead to the multiple-definitions error.
To prevent that, this declaration is not treated as a definition and you must define it yourself later.

Template inheritance in C++ and undefined symbols on Xcode

I have seen many related questions to this problem, but after carefully following advice from members, my problem still persists. The code is quite simple. I only have the following header file ("instrument.h"), which contains the base class and the template class:
#include <stdio.h>
#include <string>
using namespace std;
class Instrument
{
public:
Instrument();
virtual void print() const = 0;
};
template <class parameter> class Equity : public Instrument
{
public:
Equity();
virtual void print() const;
};
Now, in my main function on main.cpp I only do the following:
#include "instrument.h"
#include <iostream>
int main() {
Equity<double> pb;
return 0;
}
Well, I get the very well-known error:
Undefined symbols for architecture x86_64:
"Equity<double>::Equity()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have already changed in Build Settings the C++ standard library to libstdc++, also to default compiler, and so on. Do I have a problem with my project settings? Is perhaps the template wrongly implemented? I was thinking I should also have a instrument.cpp file, but then again definitions for templates must be kept in the header file so that would probably crash too.
Thanks in advance
You declared the default constructors for both Instrument and Equity but defined them nowhere.
Alter their definitions appropriately:
public:
Equity() = default; // Or {} in pre-C++11
// ^^^^^^^^^
(And equivalently for Instrument)
You can also completely omit the declarations of any default constructors for now since you didn't declare any other constructors in both Equity and Instrument and the default constructors will be generated automatically.

Accesing staic variable from static function C++

class A {
public:
static int i;
static int inc();
};
int A::inc() {
i++;
return i;
}
int main() {
A::inc();
}
As we know that we can access static member variables from static method.
But this code is giving me a error
"A::i", referenced from:
A::inc() in ccn8PKhC.o
ld: symbol(s) not found for architecture x86_64
Can some one please tell me what am i missing
This is a linker error. You've declared the variable, but you haven't defined it, so the linker can't find it.
Add this:
int A::i = 0;
somewhere globally in a .cpp file (outside your class and any function) to define it. If you have multiple .cpp files in your project, the definition needs to be in only one of them.

Undefined symbols for architecture x86_64 when compiling c++

I am continuously getting that error, and it's driving me crazy!
Undefined symbols for architecture x86_64:
"SSResourcesDepot::_sharedInstance", referenced from:
SSResourcesDepot::sharedInstance() in SSResourcesDepot.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My code looks like:
#ifndef SolarSystem_SSResourcesDepot_h
#define SolarSystem_SSResourcesDepot_h
#include "SSResource.h"
/* SSResourcesDepot is implemented as a Singleton Depot that keeps track of all
* requested Resource objects, and avoid loading them twice in memory. */
class SSResourcesDepot {
SSResourcesDepot() {};
SSResourcesDepot(SSResourcesDepot const&){};
SSResourcesDepot& operator=(SSResourcesDepot const&){};
static SSResourcesDepot* _sharedInstance;
SSResource* _search(std::string resourceName);
SSResource* _load(std::string resourceName);
public:
static SSResourcesDepot* sharedInstance();
SSResource* requestResource(std::string resourceName);
};
#endif
and:
#include <iostream>
#include "SSResourcesDepot.h"
#pragma mark Public methods
SSResourcesDepot* SSResourcesDepot::sharedInstance() {
if (SSResourcesDepot::_sharedInstance == 0) {
SSResourcesDepot::_sharedInstance = new SSResourcesDepot();
}
return SSResourcesDepot::_sharedInstance;
}
SSResource* SSResourcesDepot::requestResource(std::string resourceName) {
SSResource *resource = this->_search(resourceName);
if (resource == NULL) resource = this->_load(resourceName);
return resource;
}
#pragma mark Private methods
SSResource* SSResourcesDepot::_search(std::string resourceName) {
return NULL;
}
SSResource* SSResourcesDepot::_load(std::string resourceName) {
return NULL;
}
It seems completely functional to me, but Apple-O-Matcher keeps complaining, and it doesn't let me compile ... :-S
Thanks in advance!
You didn't initialize your static member.
Add
SSResourcesDepot* SSResourcesDepot::_sharedInstance = NULL;
to your implementation file.

C++ Singleton in Xcode

I'm trying to make a Singleton class in C++ with Xcode. It's a really basic class and I get a linker error that I don't understand. Can any1 help please?
Here is the class header file :
#ifndef _NETWORK_H_
#define _NETWORK_H_
#include <iostream>
#include <list>
#include "Module.h"
using namespace std;
/*
* Assume only one network can run at a time
* in the program. So make the class a singleton.
*/
class Network {
private:
static Network* _instance;
list<Module*> _network;
public:
static Network* instance();
};
#endif
Here is the impl file :
#include "Network.h"
Network* Network::instance() {
if (!_instance)
_instance = new Network();
return _instance;
}
Here is the compiler error :
Undefined symbols for architecture x86_64:
"Network::_instance", referenced from:
Network::instance() in Network.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You need to declare actual storage for Network::_instance somewhere. Likely the impl. file.
Try adding to your impl file:
Network *Network::_instance=0;
You need to define your instance in the implementation file:
#include "Network.h"
Network *Network::_instance;
The static Network *_instance; declaration just says that there exists a Network::_instance somewhere. You must provide a single definition somewhere for it to actually exist.