2010-11-30 8 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; 
} 

위 코드에서 다른 클래스의 typedef 함수 포인터를 사용하려고합니다. 그림과 같이 컴파일되지 않지만 다른 두 행 중 하나의 행에서 Test<A>::Func f, 행 대신 주석 처리를 제거하면 잘 컴파일됩니다! C++에서 할 수없는 일인가요? 어떤 구문이 필요합니까?템플릿 멤버 함수 typedefs 컴파일되지 않습니다

그램의 ++ 4.4.3를 사용하여, 나는 이름 Test<A>::Func 종속 이름이며 요하네스을 확인해야합니다 대한 자세한 설명은 typename

typename Test<A>::Func f, 

로 시작해야

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

[이 답변] (http://stackoverflow.com/questions/610245/where-to-put-the-template-and-typename-on-dependent-names/613132#613132) by Johannes to 관련된 질문은'typename'과 의존적 이름에 대해 알아야 할 모든 것을 설명하고 조금 더 설명 할 것입니다. – sbi

답변

관련 문제