Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
超载>&燃气轮机;c+中日期类的运算符+;导致无限循环 我试图在C++中为一个日期类重载>运算符,但是当运行进入第一个if语句时它会在无限循环中,你能帮助我吗?< /P> //operator istream& operator >>(istream& is,CustomDate& d){ int day,month,year; char ch1,ch2; string test; is>>skipws>>day>>ch1>>month>>ch2>>year; if(!is){ is.clear(ios_base::failbit); return is; } if(ch1!='/' || ch2!='/') error("invalid date input"); d = CustomDate(day,month,year); return is; }_C++_Date_Overloading_Infinite_Istream - Fatal编程技术网

超载>&燃气轮机;c+中日期类的运算符+;导致无限循环 我试图在C++中为一个日期类重载>运算符,但是当运行进入第一个if语句时它会在无限循环中,你能帮助我吗?< /P> //operator istream& operator >>(istream& is,CustomDate& d){ int day,month,year; char ch1,ch2; string test; is>>skipws>>day>>ch1>>month>>ch2>>year; if(!is){ is.clear(ios_base::failbit); return is; } if(ch1!='/' || ch2!='/') error("invalid date input"); d = CustomDate(day,month,year); return is; }

超载>&燃气轮机;c+中日期类的运算符+;导致无限循环 我试图在C++中为一个日期类重载>运算符,但是当运行进入第一个if语句时它会在无限循环中,你能帮助我吗?< /P> //operator istream& operator >>(istream& is,CustomDate& d){ int day,month,year; char ch1,ch2; string test; is>>skipws>>day>>ch1>>month>>ch2>>year; if(!is){ is.clear(ios_base::failbit); return is; } if(ch1!='/' || ch2!='/') error("invalid date input"); d = CustomDate(day,month,year); return is; },c++,date,overloading,infinite,istream,C++,Date,Overloading,Infinite,Istream,这是调用它的函数 CustomDate Menu::inputDate(){ CustomDate date; cout<<"Input your departure date"<<endl; cin>>date; if(!cin){ error("invalid date format"); } return date; } CustomDate菜单::inputDate(){ 海关日期; c

这是调用它的函数

CustomDate Menu::inputDate(){
    CustomDate date;
    cout<<"Input your departure date"<<endl;
    cin>>date;
    if(!cin){
        error("invalid date format");
    }
    return date;
}
CustomDate菜单::inputDate(){
海关日期;

cout我会重新编写您的示例,因为它依赖于能够输入多个项目,即int char int。我会输入一个字符串。然后使用stringstream解析日期并检查其格式

所以可能是这样的:

istream& operator >>(istream& is,CustomDate& d){
    string dateStr;
    is>>dateStr;
    istringstream iss(dateStr);
    int day,month,year; 
    char ch1,ch2;
    if(!(iss>>day>>ch1>>month>>ch2>>year)){
        error("invalid date input");
        return is;
    }
    if(ch1!='/' || ch2!='/'){
        error("invalid date format use m/d/y");
        return is;
    }
    d = CustomDate(day,month,year);
    return is;
}

我不确定这是否会修复任何无限循环,但这可能是检查输入的更好方法。

正如我在评论中所说:

“clear()函数应该清除流”是什么意思?它不会丢弃流内容,因此如果流中有垃圾(例如无法解析为int的字符“a”),它将永远不会“清除”垃圾,只会继续重试。我认为问题在于clear不会做你认为它做的事情

如果流提取运算符不能提取整数或分隔符错误,则仅抛出failbit,而不是抛出流提取运算符的异常(也可以尝试使用更多的空格以帮助代码更具可读性):

然后在
inputDate

CustomDate inputDate(){
    CustomDate date;
    cout << "Input your departure date" << endl;
    if (cin >> date)
      return date;

    // something went wrong
    if (cin.eof())
        error("No more data in stream");
    else // cin.fail() is true, probably invalid date format
    {
        // clear error and discard input up to next newline
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        error("invalid date format");
    }
}
CustomDate inputDate(){
海关日期;
(日期)
返回日期;
//出了点问题
if(cin.eof())
错误(“流中没有更多数据”);
else//cin.fail()为true,可能是无效的日期格式
{
//清除错误并放弃输入直到下一换行
cin.clear();
cin.ignore(数值限制::max(),'\n');
错误(“无效日期格式”);
}
}

您的代码有许多错误。
do…try…catch
循环可能是无限循环,也可能是不正确的

如果您在输入流上启用了异常,并且流提取语句中的解析在重载的
运算符>>
中失败,那么您将得到一个无限循环。您的代码永远不会重置流,因此一旦出现解析错误,您的代码将永远卡在循环中


如果您没有启用异常,并且输入被破坏到足以使流提取语句以某种方式将流标记为“坏”的程度,该怎么办?没有启用异常,因此不会引发异常。您的代码(在流提取调用之后立即使用if语句的then分支)将执行。这也不会引发异常。
do…try…catch
将成功。在这里,您的代码错误地接受错误的输入作为有效输入。

不要尝试缩进添加了`的代码段,只需使用代码块按钮。您能显示重载运算符>>而不是重载运算符吗?您键入的输入是什么?这是错误的还有一点让人惊讶的是,当出现错误时,他会清除错误状态。因此,任何使用
>
运算符的人都会大吃一惊。@pippin1289它不是一个变量,而是一个操纵器函数。真正的问题是他为什么要使用它,因为
>
默认情况下会跳过前导空格。这可能会起作用,但它会给出me编译错误C2065“数字限制”:未声明的标识符
为。清除(ios\u base::failbit)
不会清除流的
故障位
。它设置
故障位
并清除
坏位
故障位
istream& operator >>(istream& is, CustomDate& d){
    int day, month, year;
    char ch1, ch2;
    if (is >> day >> ch1 >> month >> ch2 >> year)
    {
        if (ch1 == '/' && ch2 == '/')
            d = CustomDate(day, month, year);
        else
            is.setstate(ios::failbit);
    }
    return is;
}
CustomDate inputDate(){
    CustomDate date;
    cout << "Input your departure date" << endl;
    if (cin >> date)
      return date;

    // something went wrong
    if (cin.eof())
        error("No more data in stream");
    else // cin.fail() is true, probably invalid date format
    {
        // clear error and discard input up to next newline
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        error("invalid date format");
    }
}