2017-07-19 41 views
3

我正在編譯使用C++數學庫Eigen3的C++庫。但是,下面的代碼與VC2013編譯時引入一些語法錯誤:是C++類遵循模板關鍵字

template <typename Derived> 
    inline Eigen::Transform<typename Derived::Scalar, 3, Eigen::Isometry> v2t(const Eigen::MatrixBase<Derived>& x_) { 
    Eigen::Transform<typename Derived::Scalar, 3, Eigen::Isometry> X; 
    Eigen::Matrix<typename Derived::Scalar, 6, 1> x(x_); 
    X.template linear() = quat2mat(x.template block<3,1>(3,0)); 
    X.template translation() = x.template block<3,1>(0,0); 
    return X; 
    } 

錯誤消息如下:

Error C2059 syntax error : 'template'  
Error C2039 'X' : is not a member of 'Eigen::Transform<float,3,1,0>'   
Error C2059 syntax error : 'template'  
Error C2039 'X' : is not a member of 'Eigen::Transform<float,3,1,0>'  

我還從來沒有見過的代碼一樣,X.template所以我不知道我怎麼可以這樣做來糾正這個編譯錯誤。有任何想法嗎?

+2

您使用的是什麼版本的特徵?我抓住了最新的副本,找不到那個代碼......那個庫中的代碼是你編譯的,然後引用Eigen? ..''X.template linear()'應該像'X.template linear ()'或者只是簡單的'X.linear()'.. – txtechhelp

+0

@txtechhelp我不編譯Eigen,它是一個庫正在使用Eigen http://jacoposerafin.com/nicp/ – feelfree

+0

'X.template'和'x.template'無效C++;你打算這些表達意味着什麼? –

回答

3

template關鍵字應在此處使用的模板和比較運算,例如:

struct X { 
    template <int A> 
    void f(); 
}; 

template <class T> 
void g() { 
    T t{}; 
    t.f<4>(); // Error - Do you want to compare t.f with 4 
       // or do you want to call the template t.f ? 
} 

這裏需要t.template f<4>()爲「歧義」之間的歧義。您使用的庫的問題是Eigen::Transform<...>::linear不是模板成員函數,因此template關鍵字在此處不是必需的,因此不應使用(我認爲)。

[temps.name#5]

用關鍵字template前綴的名稱應爲模板id或名稱應當是指一類模板。 [注意:關鍵字template可能不適用於類模板的非模板成員。末端 注] [...]

MSVC是正確的,Eigen::Transform<...>::linear是一個類模板的非模板成員,所以template關鍵字不宜應用。從標準下面的例子應該形成不良的,但用gcc和鏗鏘編譯得很清楚:

template <class T> struct A { 
    void f(int); 
    template <class U> void f(U); 
}; 

template <class T> void f(T t) { 
    A<T> a; 
    a.template f<>(t); // OK: calls template 
    a.template f(t); // error: not a template-id 
} 

已經有關於這對於你github使用圖書館的開放問題,但未經作者任何答案...你可以自己更新頭文件(ncip/bm_se3.h),也可以分叉項目並在github上進行拉取請求。