2010-11-30 4 views
4
#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; 
} 

In dem obigen Code versuche ich, einen Funktionszeiger Typedef aus einer anderen Klasse zu verwenden. Wie gezeigt, kompiliert es nicht, aber mit einer der beiden anderen Zeilen unkommentiert anstelle der Test<A>::Func f, Zeile kompiliert es gut! Kann ich das in C++ nicht machen? Welche Syntax wird benötigt?Templateed Member-Funktion typedefs wird nicht kompiliert

Mit g ++ 4.4.3, bekomme ich

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" 
+2

[Diese Antwort] (http://stackoverflow.com/questions/610245/where-to-put-the-template-and-typename-on-dependent-names/613132#613132) von Johannes Eine verwandte Frage wird alles erklären, was es über 'Typname' und abhängige Namen zu wissen gibt und dann noch ein bisschen mehr. – sbi

Antwort