非静态成员引用必须与特定对象Singleton类C ++相关(A nonstatic member reference must be relative to a specific object Si

编程入门 行业动态 更新时间:2024-10-27 02:26:19
静态成员引用必须与特定对象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.a

struct 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.a

struct abcd and object of type abcd named abcd is really confusing. Probably it should be

struct { int a; } abcd;

更多推荐

本文发布于:2023-07-22 01:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   对象   成员   nonstatic   Singleton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!