2011-04-17 56 views
7

我環顧四周尋找解決方案,發現了很多關於循環引用和名稱空間問題的解決方案(既不適用於我的情況) ,但沒有像我遇到的問題。C++:」錯誤:在繼承模板類之前'{'token''期待的類名稱

我有一個模板類中定義和數學/ matrix.h實現:

template<class T> 
class Matrix 
{ 
public: 
    // constructors, destructors and what not... 
}; 

我還有另外一個模板類定義和數學實行/ vector.h

#include <maths/matrix.h> 

template<class T> 
class Vector : public Matrix 
{ 
public: 
    // constructors, destructors and what not... 
}; 

我得到這個錯誤「預期class_ name在vector.h中的{'token'之前,這實際上是在擾亂我。這與matrix.h和vector.h不在一個數學子文件夾中有什麼關係,因爲我可以在應用程序的其他部分使用matrix.h而沒有任何問題。我認爲這與Matrix是一個模板化類有關,因爲當我將Vector作爲非模板化類的子類(例如SomeClass.h)時,所有編譯都可以。

非常感謝任何人,可以幫助:)

回答

11

您需要從具體的類,即繼承Matrix<T>,不僅Matrix

template<class T> 
class Vector : public Matrix<T> 
{ 
    … 
}; 
+0

OMG我是這樣的小菜!謝謝你的工作:) – 2011-04-17 14:12:49

5

你錯過了兩件事情。

template<typename T> 
class Vector : public Matrix <T> //<----- first : provide the type argument 
{ 

}; //<-------- second : semi-colon (same from Matrix class also) 
+0

非常感謝它的工作:) – 2011-04-17 14:13:12