对非静态成员的非法引用

问题描述:

我正在尝试引用来自与当前类别不同的​​类的cstring mycustompath对非静态成员的非法引用

CString test = CBar::mycustompath + _T("executables\\IECapt"); 

但我得到这个错误,而不是:

错误C2597:非法引用非静态成员 'C杆:: mycustompath' C:\工作\ b.cpp 14

如何解决这个问题?

这意味着mycustompath是特定CBar对象的属性,而不是CBar类的属性。你需要实例化一个C杆类

CBar* myBar = new CBar(); 
CString test = myBar->mycustompath + _T("executables\\IECapt"); 

或引用一个你已经拥有或者,如果mycustompath不按C杆对象不同,你可以在类将其更改为静态:

class CBar 
{ 
public: 
    static CString mycustompath; 
} 

这表示CBar::mycustompath不是CBar的静态成员变量。您将必须创建一个实例CBar才能访问它:

CBar bar; 
CString test = bar.mycustompath + _T("executables\\IECapt");