简体   繁体   English

使用模板类时的内部编译错误

[英]internal compiling error when using template class

I am trying to migrate a project from VS2008 to VS2013, The project compiles and works in VS2008, unfortunately I suddenly receive an "An internal error has occurred in the compiler" when compiling the same code with VS2013. 我正在尝试将项目从VS2008迁移到VS2013,该项目在VS2008中编译和工作,不幸的是我在使用VS2013编译相同的代码时突然收到“编译器中发生内部错误”。 The error occurs when trying the call a method of a template class 尝试调用模板类的方法时发生错误

void func(const CMatrix<CSegment>& segments) const
{
    int row, col;
    row = segments.NumOfRows(); //error points to here
    col = segments.NumOfColumns(); // if I remove the above line then the error points to here
}

and CMatrix is defined like so: 和CMatrix的定义如下:

class CBaseMatrix
{
public:
    CBaseMatrix() {};
    virtual ~CBaseMatrix() {};
    virtual void Resize(const int row, const int col) = 0;
    virtual inline int Size()   const = 0;
    virtual inline int NumOfColumns() const = 0;
    virtual inline int NumOfRows() const = 0;
};


template <class T> 
class CMatrix : public CBaseMatrix
{   
public:

    CMatrix() : 
    m_rawBuff(0),
    m_rawBuffSize(0), 
    m_columns(0), 
    m_rows(0), 
    m_data(0) 
    {};

CMatrix(const CMatrix& matrix):
{
    *this = matrix;
};

    inline int NumOfColumns()   const {return(m_columns);};

    inline int NumOfRows()  const {return(m_rows);};

    inline int Size()   const {return(m_data.size());};

private:
    int m_columns;
    int m_rows;
    vector<T> m_data;
    T*  m_rawBuff ;
    int m_rawBuffSize ;

};

I cannot find any problem with the code, and the error itself is not very informative. 我发现代码没有任何问题,错误本身并不是很有用。

I hope that I miss something or that someone encountered a similar problem and has an idea how to solve it. 我希望我能错过某些东西,或者有人遇到类似的问题,并且知道如何解决它。

Thanks. 谢谢。

I have no idea how the code would work under any compiler as: 我不知道代码如何在任何编译器下工作:

void func(const CMatrix<CSegment>& segment) const
{
    int row, col;
    row = segments.NumOfRows(); //error points to here
    col = segments.NumOfColumns(); 
}

Passes in a variable called segment and the code tries to access one called segment s . 通过在一个变量被称为片段和代码试图访问一个称为段s。

Try changing the parameter name to segments. 尝试将参数名称更改为段。

Well, I found out what the problem was, notice that in the original code: 好吧,我发现了问题所在,请注意原始代码:

CMatrix(const CMatrix& matrix):
{
    *this = matrix;
};

I have ":" after the constructor, but the initialization list is empty, Somehow that caused the compiler to crush. 我在构造函数之后有“:”,但初始化列表为空,不知何故导致编译器粉碎。

after removing them: 删除后:

CMatrix(const CMatrix& matrix)
{
    *this = matrix;
};

everything works! 一切正常!

Note that I submitted the error to Microsoft, and hopefully they will take care of this in the next update, but in the meanwhile, I leave my solution here in case anyone else encounter this problem. 请注意,我已将错误提交给Microsoft,并希望他们会在下次更新时处理此问题,但与此同时,我将解决方案保留在此处,以防其他人遇到此问题。

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

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