2012-04-19 106 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