【发布时间】:2014-11-24 09:46:31
【问题描述】:
我想将函数作为参数传递给模板化函数,而不需要任何间接。为了实现这一点,我创建了两个嵌套结构,每个都定义了我希望传递给模板化函数的函数。每个结构都从外部class B访问数据成员:
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;
};
}
我得到的编译器错误几乎都是格式:
错误:非静态数据成员 B::d 的使用无效
用于访问类数据成员的结构中的行以及在B 中声明数据成员的行。
【问题讨论】:
-
你不应该像这样调用templatedFunction中的函子吗:
t()? -
谢谢 - 必须从头开始输入代码。
-
回滚了最新的编辑,因为它使已发布的回答无效。 不要那样做。请不要在发布答案后更改代码或问题。