【问题标题】:'domain_error' in namespace 'std' does not name a type命名空间“std”中的“domain_error”未命名类型
【发布时间】:2016-09-17 14:30:55
【问题描述】:

我正在尝试实现一个通用矩阵,但在我的代码中每次声明 domain_error 时都会出现错误。

错误:

Matrix.h:31:60: error: 'domain_error' in namespace 'std' does not name a type
     Matrix operator+(const Matrix &other) const throw(std::domain_error);

 Matrix<T> Matrix<T>::operator+(const Matrix &other) const throw(std::domain_error)
                                                                      ^
Matrix.hpp:94:27: error: 'domain_error' in namespace 'std' does not name a type
             } catch (std::domain_error e)
                           ^
Matrix.hpp:96:23: error: 'e' was not declared in this scope
                 throw e;
                       ^

例如运算符'+':

矩阵.h:

#include <exception>
#include <vector>
#include <iostream>
#include <cstdlib>
typedef typename std::vector<T> genericVector;

Matrix operator+(const Matrix &other) const throw(std::domain_error);

Matrix.hpp:

template<class T>

Matrix<T> Matrix<T>::operator+(const Matrix &other) const throw(std::domain_error)
{
    if (!compareSize(*this, other)) // check if matricies have the same dimensions
    {
        throw std::domain_error(
                "illegal use of operator '+' on matrices with different dimensions.");
    }

    genericVector temp; // initialize a new vector<T>
    for (unsigned int i = 0; i < _rows; ++i)
    {
        for (unsigned int j = 0; j < _cols; ++j)
        {
            try
            {
                temp.push_back(_cells[_cols * i + j] + other(i, j)); // sum matricies to vector
            } catch (std::domain_error e) // if thrown domain error..
            {
                throw e;
            }
        }
    }

    return Matrix(_rows, _cols, temp); // return the sum of matrices
}

我还包括..

谢谢大家!

【问题讨论】:

标签: c++ exception matrix operator-overloading generic-programming


【解决方案1】:

std::domain_error&lt;stdexcept&gt; 中声明。

您的Matrix.hpp 使用此符号,而#include 没有此必需的头文件。因此,除非翻译单元碰巧已经包含它,否则尝试包含 Matrix.hpp 将导致此编译错误。

【讨论】:

    【解决方案2】:

    std::domain_error 类在标头&lt;stdexcept&gt; 中定义。

    【讨论】:

      猜你喜欢
      • 2013-05-27
      • 2012-11-08
      • 2012-12-20
      • 2015-05-08
      • 1970-01-01
      • 2017-06-06
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多