2010-10-12 14 views
1

爲什麼此代碼導致編譯器錯誤?mem_fun_ref問題

#include <iostream> 
#include <algorithm> 

using namespace std; 

class X 
{ 
    public: 
     void Print(int x) 
     { 
      cout << x << endl; 
     } 
}; 

int main() 
{ 
    X x; 
    mem_fun_ref<void, X, int>(&X::Print) p; 
}; 

錯誤 main.cpp:18: error: expected ; before p

回答

2

mem_fun_ref是一個函數模板,所以它沒有指定類型。

mem_fun_ref<void, X, int>(&X::Print)是一個函數調用,返回一個值,所以沒有任何意義,它後面有一個p

該函數調用的返回值是mem_fun1_ref_t<void, X, int>,以防您正在尋找。

2

你打算寫

mem_fun1_ref_t<void, X, int> p(&X::Print); 
      ^^^^    ^^^ 

呢? mem_fun_ref不是類模板,而是函數模板。