[英]Templated member function typedefs won't compile
#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,我得到
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,
有關更詳細的說明,請查看以下答案中的約翰內斯說明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.