(C ++)错误:'invalid

编程入门 行业动态 更新时间:2024-10-27 16:35:28
(C ++)错误:'invalid_argument'未在此范围内声明((C++) error: 'invalid_argument' was not declared in this scope)

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

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

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

最满意答案

std::invalid_argument在头文件<stdexcept>定义。 包括它。

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

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

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

更多推荐

本文发布于:2023-08-07 03:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   invalid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!