我希望將函數作爲參數傳遞給模板化函數,而沒有任何間接性。爲了實現這個,我創建了兩個嵌套的結構體,每個都定義了我希望傳遞給模板化函數的函數。每個結構由外class B
訪問數據成員:C++非靜態數據成員的無效使用
namespace A{
class B{
public:
B();
template <typename T>
void templatedFunction(T t){
//I pass one of the struct objects in to here, to invoke the desired function
t();
}
private:
struct X{
void operator(){
do();
}
void do(){
//Accesses the data members of class B
e->doSomething();
}
};
struct Y{
void operator(){
do();
}
void do(){
//Accesses the data members of class B
d.doSomething();
}
};
C* c;
D d;
E* e;
};
}
和編譯器錯誤,我得到的是幾乎所有的格式:
error: invalid use of non-static data member B::d
用於訪問類的數據結構中的行成員和在B
中聲明數據成員的行。
不應該像這樣在模板函數中調用函數:'t()'? – 2014-11-24 09:50:48
感謝 - 必須從頭開始輸入代碼。 – user997112 2014-11-24 09:53:27
回滾最新的編輯,因爲它使已發佈的anaswer失效。 **不要那樣做**。請在答案發布後不要更改代碼或問題。 – 2014-11-24 10:04:28