2012-04-19 130 views
1

我有下面的類的typedef模板類錯誤

template < int rows, int columns > 
class Matrix 
{ 
    //stuff 
}; 

我做了以下內容:

typedef Matrix<4,4> Matrix3D; 

但是我得到的錯誤,當我宣佈在另一大類如下:

class Transform3D 
{ 
public: 
    Matrix3D matrix; 
     //some other stuff 
}; 

我看到的錯誤是:

error C2146: syntax error : missing ';' before identifier 'matrix' 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 

所有這些都是在這行7:

Matrix3D matrix; 

這是在2010年VS可能是什麼問題?

+0

哦,那末段差分號:) – 2012-04-19 05:17:10

+0

對不起!將它錯誤地複製到了stackoverflow中,它存在於我的VS代碼中 – 2012-04-19 05:18:00

+0

對'class Transform3D'可見'Matrix'及其''typdef'嗎? – iammilind 2012-04-19 05:19:42

回答

0

從你的解釋我認爲以下設置:

stdafx.h中

// .. 
typedef Matrix<4,4> Matrix3D; 
// .. 

Matrix.h

template < int rows, int columns > class Matrix { /*...*/ }; 

Transform.h

class Transform3d { Matrix3D matrix; /*...*/ }; 

Transform.cpp

#include "stdafx.h" 

如果是這種情況下,類的Transform3D似乎並不矩陣模板的定義,(我希望在stdafx.h中typedef的產生編譯錯誤,但我不是很熟悉的Visual Studio預編譯頭)。

你應該#包括文件Matrix.h文件Transform.h和stdafx.h中在Transform.h移動的typedef。或者......你應該包括Matrix.h Stdafx.h中,但我會做,只有當你的頭文件是足夠穩定(保證你還是利用預編譯頭)。

我的首選方式:

stdafx.h中

// .. 
// typedef Matrix<4,4> Matrix3D; -- removed from here 
// .. 

Matrix.h

template < int rows, int columns > class Matrix { /*...*/ }; 

變換。h

#include "Matrix.h" 

typedef Matrix<4,4> Matrix3D; 

class Transform3d { Matrix3D matrix; /*...*/ }; 
+0

是的,我做了類似的事情(我把它放在Matrix.h中的Matrix類之後)並且它可以工作。謝謝 :) – 2012-04-19 06:02:59

0

我已經創建的項目只有一個文件,它沒有編譯

template < int rows, int columns > 
class Matrix 
{ 
    //stuff 
}; 

typedef Matrix<4,4> Matrix3D; 

class Transform3D 
{ 
public: 
    Matrix3D matrix; 
    //some other stuff 
}; 

void main() 
{ 
} 

因此,我認爲這個問題是有關預編譯頭的使用。你能告訴更多關於你的文件是如何組織的?

+0

每個類都有自己的頭文件和一個cpp文件,其中類是在頭文件中定義的,而類函數是在相應的cpp文件中定義的。所有這些文件都只有#include「stdafx.h」。 stdafx.h包含每個類的頭文件的包含,並且還包含typedef Matrix <4,4> Matrix3D;在文件末尾 – 2012-04-19 05:40:29

+0

解決了它,這要歸功於這個http://stackoverflow.com/questions/7870162/why-isnt-this-typedef-working。我太早了(在課堂宣佈之前)。感謝您的幫助:) – 2012-04-19 05:47:47