I'm getting an Undefined reference error message, on this statement:
GlobalClass *GlobalClass::s_instance = 0;
Any ideas? Code is shown below:
================================================
#ifndef GLOBALCLASS_H_
#define GLOBALCLASS_H_
#include <string>
class GlobalClass {
public:
std::string get_value();
void set_value(std::string);
static GlobalClass *instance();
static GlobalClass *s_instance;
private:
std::string m_value;
};
#endif /* GLOBALCLASS_H_ */
===============================================
#include <string>
#include "GlobalClass.h"
/*
GlobalClass(int v = 0)
{
m_value = v;
}
*/
static GlobalClass *s_instance;
std::string GlobalClass::get_value()
{
return m_value;
}
void GlobalClass::set_value(std::string v)
{
m_value = v;
}
static GlobalClass *instance() {
if (!s_instance)
s_instance = new GlobalClass;
return s_instance;
}
===========================================================
#include <iostream>
#include "GlobalClass.h"
using namespace std;
int main() {
GlobalClass::s_instance = 0;
std::string myAddress = "abc";
GlobalClass::instance()->set_value(myAddress); \\ <=== compiler error
std::cout << "====>address is is " << GlobalClass::instance()->get_value()
<< std::endl;
return 0;
}