我希望将函数作为参数传递给模板化函数,而没有任何间接性。为了实现这个,我创建了两个嵌套的结构体,每个都定义了我希望传递给模板化函数的函数。每个结构由外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