Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(C++) error: 'invalid_argument' was not declared in this scope

Tags:

c++

c++11

I am using Eclipse C/C++ with the MinGW compiler. I have added the flag -std=c++11 to the Miscellaneous GCC C Compiler Settings under C/C++ Build in the project properties. I know its probably a simple thing, but I cannot resolve this error.

Date.h

#include <iostream>
using namespace std;

class Date {
public:
    Date(int m = 1, int d = 1, int y = 1900);
    void setDate(int, int, int);
private:
    int month;
    int day;
    int year;

    static const int days[];
};

Date.cpp

#include <iostream>
#include <string>
#include "Date.h"
using namespace std;

const int Date::days[] = {
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

Date::Date(int month, int day, int year){
    setDate(month, day, year);
}

void Date::setDate(int month, int day, int year){
    if (month >= 1 && month <= 12){
        this->month = month;
    } else {
        // error
        invalid_argument("month must be within the range [0, 12]");
    }
    ...
}

Compiler message:

..\Date.cpp: In member function 'void Date::setDate(int, int, int)':
..\Date.cpp:25:60: error: 'invalid_argument' was not declared in this scope
   invalid_argument("month must be within the range [0, 12]");
like image 266
ChrisMcJava Avatar asked Dec 06 '22 22:12

ChrisMcJava


1 Answers

std::invalid_argument is defined in the header <stdexcept>. Include it.

You probably also mean to throw the object rather than just construct it:

throw invalid_argument("month must be within the range [1, 12]");
like image 152
T.C. Avatar answered Dec 31 '22 02:12

T.C.