简体   繁体   English

C++ cout 未按所需顺序打印

[英]C++ cout not printing in the desired order

there's a operator overloading that I made for some assignment and it's supposed to print whether two intervals has anything in common.我为某些赋值做了一个运算符重载,它应该打印两个间隔是否有任何共同点。 And the output isn't printing in the right order because as i've noticed it's printing what i want during the call of the operator overloading function and only then the actual text,and after that it still returns and prints 'Invalid Interval' even tho i didnt even return any.并且输出没有以正确的顺序打印,因为我注意到它在调用运算符重载函数期间打印我想要的内容,然后才打印实际文本,之后它仍然返回并打印“无效间隔”甚至我什至没有返回任何。 on the main :主要:

    cout << "interval13 && interval24 = " << (interval13 && interval24) << endl;
    cout << "interval13 && interval45 = " << (interval13 && interval45) << endl;
    cout << "interval24 && interval45 = " << (interval24 && interval45) << endl;
    cout << "interval24 && interval13 = " << (interval24 && interval13) << endl;
    cout << "interval45 && interval13 = " << (interval45 && interval13) << endl;
    cout << "interval45 && interval24 = " << (interval45 && interval24) << endl;

The function:功能:

template <class T>
Interval<T> Interval<T>::operator&&(Interval<T> &i1) {
    if (b < i1.a) {
        cout<<"EMPTY";
        exit;
    }
    else if (b == i1.a) {
        cout << "EMPTY";
        exit;
    }
    else if (a > i1.b) {
        cout << "EMPTY";
        exit;   
    }
    else if (a == i1.b) {
        cout << "EMPTY";
        exit;
    }
    else {
        if (a<i1.a){
            if (b < i1.b)
                return Interval<T>(i1.a, b);
            else return Interval<T>(i1.a, i1.b);
        }
        if (i1.a < a) {
            if (i1.b < b)
                return Interval<T>(a, i1.b);
            else return Interval<T>(a, b);
        }
    }
}

Output:输出:

 intervall3 && interval24 = (2,3) EMPTYintervall3 && interval45 = Invalid Interval EMPTYinterval24 && interval45 = Invalid Interval interval24 && intervall3 (2,3) EMPTYinterval45 && intervall3 = Invalid Interval EMPTYinterval45 && interval24 = Invalid Interval

What did i do wrong?我做错了什么? and how can i fix that?我该如何解决? thanks.谢谢。

In your definition of the && operator, you are using the std::cout resource.&&运算符的定义中,您使用的是std::cout资源。 In the call of && , however, you need a string to pass to the high level std::cout call.然而,在&&的调用中,您需要一个字符串来传递给高级std::cout调用。 You need to rewrite your function to return an std::string object.您需要重写函数以返回std::string对象。 Try getting rid of the std::cout calls in your template function.尝试摆脱模板函数中的std::cout调用。 Usually, however, the && operator is a logical boolean operator, so you should really right a function that returns a boolean instead, given the 2 inputs.然而,通常, &&运算符是一个逻辑布尔运算符,所以你应该真正纠正一个返回布尔值的函数,给定 2 个输入。

Something like this:像这样的东西:

std::cout << cout << "interval13 && interval24 = " << (doCompare(interval13, interval24) ? "EMPTY" : "NONEMPTY") << endl; std::cout << cout << "interval13 && interval24 = " << (doCompare(interval13, interval24) ? "EMPTY" : "NONEMPTY") << endl;

where doCompare() takes to function arguments are returns bool .其中doCompare()接受函数参数是返回bool

I would try to give you a working solution, but currently, your code does not compile and I don't see your end goal nor the desired functionality.我会尝试为您提供一个可行的解决方案,但目前,您的代码无法编译,我看不到您的最终目标或所需的功能。

More issues:更多问题:

The call to exit() should only be called in main() , not in a helper function defined outside of main() .exit()的调用应该只在main()调用,而不是在main()之外定义的辅助函数中调用。 You should call return and then return whatever the function return type is (I don't know what it is you want to return).您应该调用return ,然后返回任何函数返回类型(我不知道您想返回什么)。

The << is an operator used with C++ streams like ostream and istream . <<是与 C++ 流(如ostreamistream一起使用的运算符。 The object std::cout is an output stream that outputs to the console.对象std::cout是输出到控制台的输出流。

I cannot run your code in its current state, but I suggest you do more research on operator overloading .我无法在当前状态下运行您的代码,但我建议您对运算符重载进行更多研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM