-1

I am trying to implement a generic matrix and I got an errors on each decleration of domain_error in my code..

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;
                       ^

for example the operator '+':

Matrix.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
}

Also I include ..

thanks all!

Eliav
  • 29
  • 5
  • 3
    #include –  Sep 17 '16 at 14:34
  • Note: throw exception specifications are deprecated: http://stackoverflow.com/questions/13841559/deprecated-throw-list-in-c11 –  Sep 17 '16 at 14:38

2 Answers2

2

std::domain_error is declared in <stdexcept>.

Your Matrix.hpp uses this symbol without #includeing this required header file. As such, unless the translation unit happens to already include it, trying to include Matrix.hpp will result in this compilation error.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
2

The class std::domain_error is defined in in header <stdexcept>.

BiagioF
  • 9,368
  • 2
  • 26
  • 50