我有以下课程
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;
这是在VS 2010中。可能是什么问题?
答案 0 :(得分:0)
我创建了只有一个文件的项目,并且编译了
template < int rows, int columns >
class Matrix
{
//stuff
};
typedef Matrix<4,4> Matrix3D;
class Transform3D
{
public:
Matrix3D matrix;
//some other stuff
};
void main()
{
}
因此,我认为问题与预编译头的使用有关。你能详细说明你的文件是如何组织的吗?
答案 1 :(得分: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似乎不是Matrix模板的定义,(我希望stdafx.h中的typedef生成编译错误,但我不太熟悉Visual中的预编译头文件工作室)。
你应该在文件Transform.h中#include文件Matrix.h并在Transform.h中从stdafx.h移动typedef。或者......你应该在stdafx.h中包含Matrix.h,但是只有当你的头文件足够稳定时才会这样做(以确保你仍然可以利用预编译的头文件)。
我的首选方式:
<强> stdafx.h中强>
// ..
// typedef Matrix<4,4> Matrix3D; -- removed from here
// ..
<强> Matrix.h 强>
template < int rows, int columns > class Matrix { /*...*/ };
<强> Transform.h 强>
#include "Matrix.h"
typedef Matrix<4,4> Matrix3D;
class Transform3d { Matrix3D matrix; /*...*/ };