2016-02-11 46 views
0

試圖在一個模板構造函數投的參數時,我得到一個奇怪的編譯錯誤。下面是一個小例子:奇怪的錯誤在模板的構造

#include <Eigen/Dense> 

class Plane 
{ 
public: 
    template <typename TVector> 
    Plane(const TVector& coefficients) { 
     coefficients.cast<double>(); // compiler error on this line 
    } 

// This compiles fine 
// Plane(const Eigen::Vector3d& coefficients) { 
//  coefficients.cast<double>(); 
// } 
}; 

int main(){return 0;} 

的錯誤是:

expected primary-expression before 'double' 
expected ';' before 'double' 

因爲此類從不實例(main()是空的),我認爲編譯器甚至看不到在函數模板所有的,所以我很困惑這個表達式有什麼錯誤?

回答

2

你必須使用template關鍵字:

template <typename TVector> 
Plane(const TVector& coefficients) { 
    coefficients.template cast<double>(); 
} 
+0

是的,這做的。你能說這是什麼,所以我可以查看我什麼時候需要使用它? –

+0

@DavidDoria他做到了。它是['template'關鍵字(http://en.cppreference.com/w/cpp/keyword/template) – clcto

+0

@DavidDoria看到你原來的問題,這已被標記爲重複上面的鏈接。該鏈接全面解釋了這種現象。 – iksemyonov