2013-11-09 18 views
0

的成員函數我有一個類(在wxWidgets的框架)那樣定義:C++無法公共常量字符串構件傳遞作爲參數傳遞給同一類

class SomePanel : public wxPanel{ 
public: 
    ... 
    void SomeMethod(const std::string& id){ 
     pointer->UseId(id); 
    } 

    const std::string id = "Text"; // still in public area 
    ... 
} 

別的地方在pogram我創建對一個參考此對象的實例...

mSomePanel = new SomePanel(); 

...然後我想這樣做

mSomePanel->SomeMethod(mSomePanel->id); // Compiler gives an error saying that 
             // there is no element named id. 

在(構造函數Ò我可以用這個成員變量調用相同的方法。問題在哪裏?

+1

id是SomePanel的成員,將它傳遞給自我有什麼意義? – billz

+1

粘貼的代碼不完整。 (請僅編譯粘貼) –

回答

1

忽略我以前的ramblings。 Classname :: id應該讓你的id。

mSomePanel->SomeMethod(SomePanel::id); // this should work. 

編輯補充更完整的代碼:

這正好在你的.h:

class SomePanel { 
public: 
    static const std::string id; // no need to have an id for each SomePanel object... 
}; 

這正好在您的實現文件(例如,SomePanel.cpp):

const std::string SomePanel::id = "Text"; 

現參照編號:

SomePanel::id 

另外,另一個問題可能是這樣一個事實,即一個方法的參數與成員變量的名稱相同。當您調用UseId(id)時,編譯器如何知道您指的是您的成員變量與函數的參數。嘗試更改SomeMethod()中的參數名稱。

+5

'id'既不是靜態的,也不是私有的。 – Thomas

+0

感謝您的投票。 –

+0

我試了一下,現在我在同一個地方得到編譯器錯誤:mSomePanel不是一個類或名稱空間。 - fotinsky剛纔編輯 – fotinsky

相關問題