I have three files:
api.h
:
class HttpApi{
public:
static bool postData(string json);
private:
static string remoteHost;
static string port;
static string url;
};
api.cpp
:
string HttpApi::remoteHost = Config::getInstance().getRemoteServer();
string HttpApi::port = Config::getInstance().getPort();
string HttpApi::url="/api/miner";
bool HttpApi::postData(string json)
{
//Here I print Config::getInstance.getRemoteServer(), the value is correct set here
cout<<"Start resolve "<< remoteHost<<" "<<port<<endl;
cout<<"Succeed in resolving "<<endl;
}
and finally:
int main(int argc, char** argv)
{
Config& config = Config::getInstance();
cout<<"Start loading configuration "<<endl;
config.loadConfig("config.ini");
HttpApi::postData("hello world");
}
The problem for me is that the initialization of two members remoteHost
, port
is invalid: at runtime, both are empty.
Here Config
is a singleton class and it reads values from config.ini. It has some members, such as remoteHost
, and port
.
Why are both static members empty and how can I fix it?