异常抛出:依赖名称被解析为非类型,但实例化产生类型

问题描述 投票:0回答:0

我有一个模板矩阵类:

template <typename T, int row, int col>
class Matrix{};

在类声明中我写了一个执行类,基于 std::exception:

class MatrixException: public std::exception
{
private:
    const char* errmsg;
public:
    MatrixException(const char* msg="")
        :errmsg(msg) {} 
    const char* what() const noexcept {return errmsg;}
};

class IllegalOperation: public MatrixException
{
    public:
        IllegalOperation(const char* msg="")
            : MatrixException(msg) {}
};

我尝试在植入部分的非类函数之一上使用它,如下所示:


template <typename T, int row1, int col1, int row2, int col2>
Matrix<T, row1, col2> operator*(const Matrix<T, row1, col1>& mat1, const Matrix<T, row2, col2>& mat2)
{
    if (condition)
        throw Matrix<T, row2, col2>::IllegalOperation("message");
    // do stuff
    return res;
}

我收到警告:

 In instantiation of 'Matrix<T, row1, col2> operator*(const Matrix<T, row, col>&, const Matrix<T, row2, col2>&) [with T = int; int row1 = 3; int col1 = 3; int row2 = 3; int col2 = 2]': required from here

这个错误信息:

dependent-name 'Matrix<T, row, col>::IllegalOperation' is parsed as a non-type, but instantiation yields a type

我该如何解决这个问题?

我看过这里: 我必须在哪里以及为什么必须放置“模板”和“类型名称”关键字? 然后尝试在这里和那里添加

template
这个词,但它没有用。

c++ templates throw
© www.soinside.com 2019 - 2024. All rights reserved.