简体   繁体   English

typedef模板类错误

[英]typedef template class error

I have the following class 我有以下课程

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

I am doing the following: 我正在执行以下操作:

typedef Matrix<4,4> Matrix3D;

However I am getting errors when I declare the following in another class: 但是,当我在另一个类中声明以下内容时,出现错误:

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

The errors I see are: 我看到的错误是:

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

all these are at line 7 which is: 所有这些都在第7行,即:

    Matrix3D matrix;

This is in VS 2010. What could be the issue? 这是VS 2010中的问题。可能是什么问题?

I have created project with only one file and it did compile 我创建的项目只有一个文件,并且确实可以编译

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

typedef Matrix<4,4> Matrix3D; 

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

void main()
{
}

Thus, I think the problem is related to the usage of precompiled headers. 因此,我认为问题与预编译头文件的使用有关。 Can you tell more about how your files are organized? 您能否进一步说明文件的组织方式?

From your explanation I assume the following setup: 根据您的解释,我假设以下设置:

stdafx.h stdafx.h

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

Matrix.h 矩阵

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

Transform.h 转换h

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

Transform.cpp 转换文件

#include "stdafx.h"

If this is the case, the class Transform3D doesn't seem the definition of Matrix template, (I would expect that the typedef in stdafx.h to generate a compilation error, but I am not very familiar with precompiled headers in Visual Studio). 如果是这种情况,则类Transform3D似乎不是Matrix模板的定义(我希望stdafx.h中的typedef会生成编译错误,但我对Visual Studio中的预编译头不是很熟悉)。

You should #include file Matrix.h in file Transform.h and move the typedef from stdafx.h in Transform.h. 您应该在文件Transform.h中#include文件Matrix.h,并从Transform.h中的stdafx.h中移动typedef。 Or... you should include Matrix.h in stdafx.h, but I would do that only if you that header file is stable enough (to ensure you still take advantage of precompiled headers). 或者...您应该在stdafx.h中包括Matrix.h,但是只有在您的头文件足够稳定的情况下,我才会这样做(以确保您仍然利用预编译的头)。

My prefered way: 我首选的方式:

stdafx.h stdafx.h

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

Matrix.h 矩阵

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

Transform.h 转换h

#include "Matrix.h"

typedef Matrix<4,4> Matrix3D; 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM