读书人

quot;指向成员的指针quot;编译异常,错在哪里呢

发布时间: 2012-08-14 10:39:57 作者: rapoo

"指向成员的指针"编译错误,错在哪里呢?
我写了几行代码,想通过指向成员的指针,调用类当中的一个模板函数。
我在VC10下写的程序,

C/C++ code
#include"stdafx.h"struct s{    void f(){printf("%s\n",__FUNCTION__);}    template<class Pmf>    void func{        this->*Pmf();    }};int main(void){    s s1;    s1.func(&s::f);    return 0;}

输出信息是:
1> my.cpp
1>c:\users\administrator\documents\visual studio 2010\projects\my\my\my.cpp(7): error C2182: 'func' : illegal use

of type 'void'
1>c:\users\administrator\documents\visual studio 2010\projects\my\my\my.cpp(4): error C3857: 's::func': multiple

template parameter lists are not allowed
1>c:\users\administrator\documents\visual studio 2010\projects\my\my\my.cpp(11): fatal error C1903: unable to

recover from previous error(s); stopping compilation
1>

程序错在哪里?


[解决办法]
C/C++ code
struct s{    void f(){printf("%s\n",__FUNCTION__);}    template<class Pmf>    void func(Pmf pmf){ //这里        (this->*pmf)(); //这里    }};int main(void){    s s1;    s1.func(&s::f);    return 0;} 

读书人网 >C++

热点推荐