非静态成员引用必须与特定对象Singleton类C ++相关(A nonstatic member reference must be relative to a specific object Singleton class C++)
我有一个单身课。 我想为结构成员分配一个值。 但编译器抛出一个错误作为A nonstatic member reference must be relative to a specific object 。 请帮助我,如何解决同样的问题。
class abc{ static abc* m_selfInst; public: struct abcd{ int a; }abcd; abc* GetInstance(); }; abc* abc::m_selfInst = NULL; abc* abc::GetInstance() { if(m_selfInst == NULL) { m_selfInst = new abc(); } return m_selfInst; } int main() { abc *ab = abc::GetInstance(); //Error Occurs here abc::abcd.a = 5; //Error occurs here too //cout <<ab.abcd.a << "\n"; return 0; }I have a singleton class. I would like to assign a value to a structure member. But the compiler throwing an error as A nonstatic member reference must be relative to a specific object. Please help me, how to solve the same.
class abc{ static abc* m_selfInst; public: struct abcd{ int a; }abcd; abc* GetInstance(); }; abc* abc::m_selfInst = NULL; abc* abc::GetInstance() { if(m_selfInst == NULL) { m_selfInst = new abc(); } return m_selfInst; } int main() { abc *ab = abc::GetInstance(); //Error Occurs here abc::abcd.a = 5; //Error occurs here too //cout <<ab.abcd.a << "\n"; return 0; }最满意答案
GetInstance应该是static函数。
static abc* GetInstance();对a访问应该使用abc类型的变量。
ab->abcd.astruct abcd和abcd类型的对象命名为abcd真的很让人困惑。 可能它应该是
struct { int a; } abcd;GetInstance should be static function.
static abc* GetInstance();access to a should be with variable of type abc.
ab->abcd.astruct abcd and object of type abcd named abcd is really confusing. Probably it should be
struct { int a; } abcd;更多推荐
发布评论