模板化的成员函数typedefs无法编译 [英] Templated member function typedefs won't compile
本文介绍了模板化的成员函数typedefs无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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<.> :: Func f,
行,编译良好!这是我在C ++中做不到的事情吗?需要什么语法?
In the code above, I am trying to use a function pointer typedef from another class. As shown, it does not compile, but with either of the other two lines uncommented instead of the Test< A >::Func f,
line, it compiles fine! Is this something I can't do in C++? What syntax is needed?
使用g ++ 4.4.3,我得到
Using g++ 4.4.3, I get
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
The name Test<A>::Func
is a dependent name and needs to be prefixed with typename
typename Test< A >::Func f,
要获得更详细的解释,您应该在以下答案中查看约翰内斯的解释
For a more detailed explanation you should check out Johannes explanation in the following answer
这篇关于模板化的成员函数typedefs无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文