2011-03-01 45 views
2

我正在嘗試引用來自與當前類別不同的​​類的cstring mycustompath對非靜態成員的非法引用

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

但我得到這個錯誤,而不是:

錯誤C2597:非法引用非靜態成員 'C杆:: mycustompath' C:\工作\ b.cpp 14

如何解決這個問題?

回答

6

這意味着mycustompath是特定CBar對象的屬性,而不是CBar類的屬性。你需要實例化一個C杆類

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

或引用一個你已經擁有或者,如果mycustompath不按C杆對象不同,你可以在類將其更改爲靜態:

class CBar 
{ 
public: 
    static CString mycustompath; 
} 
3

這表示CBar::mycustompath不是CBar的靜態成員變量。您將必須創建一個實例CBar才能訪問它:

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