#include <iostream>
#include <string>
using namespace std;
void printstr( const string & s ) { cout << s << endl; }
template < typename A >
class Test
{
public:
typedef void (*Func)( const A & );
};
typedef void (*Func)( const string & );
template < typename A >
void bind(
Test< A >::Func f, //<---- does NOT compile
//Func f, //<---- compiles & works!
//void (*f)( const A & ), //<---- compiles & works!
const A & a) { f( a ); }
int main( )
{
bind( printstr, string("test") );
return 0;
}
在上面的代码中,我尝试使用另一个类中的函数指针 typedef。如图所示,它没有编译,但是其他两行中的任何一行都没有注释,而不是 Test< A >::Func f,
。行,它编译得很好!这是我不能用 C++ 做的事情吗?需要什么语法?
使用 g++ 4.4.3,我得到 p>
test.cpp:20: error: variable or field "bind" declared void
test.cpp:20: error: expected ")" before "f"
test.cpp:23: error: expected primary-expression before "const"
最佳答案
名字Test<A>::Func
是从属名称,需要以 typename
为前缀
typename Test< A >::Func f,
有关更详细的解释,您应该查看以下答案中的 Johannes 解释
关于c++ - 模板化成员函数 typedef 无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4317105/