2016-04-08 71 views
0

我試圖通過派生類調用模板化的基類方法。 這是我的代碼調用模板化的基類方法編譯失敗

struct base 
{ 
    template<typename t> 
    void baseMethod(t s) 
    { 
     std::cout << s; 
    } 
}; 


struct der : public base 
{ 
}; 


int main() 
{ 
    der d; 
    d.<int>(baseMethod(12)); 
} 

編譯失敗,並指出

main.cpp: In function 'int main()': main.cpp:25:5: error: expected unqualified-id before '<' token d.(baseMethod(12)); ^main.cpp:25:6: error: expected primary-expression before 'int' d.(baseMethod(12));

我如何能解決這個問題有什麼建議?

回答

5

即使本questuion無關與繼承的事實,正確的語法將

d.baseMethod<int>(12); 

然而,即使這是沒有必要因template deduction:簡單

d.baseMethod(12); 

會工作。

+0

謝謝。語法拋給我 - 作爲定時器之後的答案 –

相關問題