#include <iostream>
#include <stdlib.h>
#include <sstream>
class api
{
private:
void psParser ()
{
std::stringstream psOutput;
psOutput << "ps --no-headers -f -p " << getpid() << " > .txt";
system (psOutput.str().c_str());
std::stringstream processInfo;
processInfo << ":"__FILE__ << ":" << __DATE__ << ":" << __TIME__ << ":";
}
public:
static std::stringstream message;
};
namespace sstreamss
{
std :: stringstream api :: message;
};
int main ()
{
api::message << "zxzx";
return 0;
}
Output:
error: definition of ‘api::message’ is not in namespace enclosing ‘api’
I want that static std::stringstream message should be accessible at a global scope, so I want this under a namespace.
What’s the way out?
I am guessing that you want to have the same instance of
api::messageaccessible across all translation units that have access toapi. Unlike plain, non-classstaticdata, which have internal linkage,staticclass members have external linkage. This means the same instance is seen everywhere. So you do not have to play any games with namespaces. A namespace wouldn’t change anything here, but it would have to enclose the wholeapiclass anyway.