gpt4 book ai didi

c++ - (C++) 错误 : 'invalid_argument' was not declared in this scope

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:00 26 4
gpt4 key购买 nike

我正在使用 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[];
};

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

最佳答案

std::invalid_argument在 header 中定义 <stdexcept> .包括它。

您可能还想 throw对象而不是构造它:

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

关于c++ - (C++) 错误 : 'invalid_argument' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28554927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com