C++ 非法引用非静态成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5153102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
illegal reference to non-static member
提问by karikari
I am trying to refer to a cstring mycustompath
from a different class from my current class.
我试图mycustompath
从我当前的班级引用不同班级的 cstring 。
CString test = CBar::mycustompath + _T("executables\IECapt");
But I got this error instead:
但我得到了这个错误:
error C2597: illegal reference to non-static member 'CBar::mycustompath' c:\work\b.cpp 14
错误 C2597:非法引用非静态成员“CBar::mycustompath”c:\work\b.cpp 14
How to fix this?
如何解决这个问题?
回答by Rup
This means that mycustompath is a property of a specific CBar object and not a property of the CBar class. You'll need to instantiate a CBar class
这意味着 mycustompath 是特定 CBar 对象的属性,而不是 CBar 类的属性。你需要实例化一个 CBar 类
CBar* myBar = new CBar();
CString test = myBar->mycustompath + _T("executables\IECapt");
or reference one you already have or, if mycustompath doesn't vary by CBar object, you can change it to static in the class:
或引用您已经拥有的一个,或者,如果 mycustompath 不因 CBar 对象而异,您可以在类中将其更改为静态:
class CBar
{
public:
static CString mycustompath;
}
回答by Bj?rn Pollex
This indicates that CBar::mycustompath
is not a static member variable of CBar
. You will have to create an instance of CBar
to access it:
这表明CBar::mycustompath
不是 的静态成员变量CBar
。您必须创建一个实例CBar
才能访问它:
CBar bar;
CString test = bar.mycustompath + _T("executables\IECapt");