(C ++)错误:' invalid_argument'在这方面没有申明

时间:2015-02-17 04:58:23

标签: c++ c++11

我正在使用Eclipse C / C ++和MinGW编译器。我已将标志-std = c ++ 11添加到项目属性中C / C ++ Build下的Miscellaneous GCC C Compiler Settings。我知道这可能是一件简单的事情,但我无法解决这个错误。

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]");
    }
    ...
}

编译器消息:

..\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]");

1 个答案:

答案 0 :(得分:9)

std::invalid_argument在标头<stdexcept>中定义。包括它。

你可能也意味着throw对象而不仅仅是构造它:

throw invalid_argument("month must be within the range [1, 12]");