0

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.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Apocaly
  • 3
  • 2
  • 1
    The evaluation order of function arguments is undefined. This is (one reason) why you should not put output logic inside operator overloads (esp. to `cout` or `cerr`). Unrelated note: you should typically not overload `operator&&`, as this strips it of sequencing, short circuiting, and implicit `bool` coercions (plus canonically it should return `bool`). This should be `operator&` instead – Cruz Jean Jan 10 '20 at 20:14
  • Also what is `exit`? Unless it's a macro `exit;` does nothing. – Cruz Jean Jan 10 '20 at 20:17
  • 1
    C++17 sorts out a lot of potential ordering problems, each `<<` is resolved before proceeding to the next `<<`, but there is still room for chaos between them. – user4581301 Jan 10 '20 at 20:18
  • What does the `operator<<` do, nothing? (That would be a hint things don't really make sense.) – aschepler Jan 10 '20 at 20:21
  • Basically the same thing as [The order of cout messages is not as expected](https://stackoverflow.com/questions/23309153/the-order-of-cout-messages-is-not-as-expected), just with a different type. – Raymond Chen Jan 10 '20 at 20:30
  • It was required on the assignment to use the exact part of that main so that means i can only do some changes to my functions in order to fix that (and also they required me to overload && so couldn't use & or something else) – Apocaly Jan 10 '20 at 22:42

1 Answers1

0

In your definition of the && operator, you are using the std::cout resource. In the call of &&, however, you need a string to pass to the high level std::cout call. You need to rewrite your function to return an std::string object. Try getting rid of the std::cout calls in your template function. 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.

Something like this:

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

where doCompare() takes to function arguments are returns 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(). You should call return and then return whatever the function return type is (I don't know what it is you want to return).

The << is an operator used with C++ streams like ostream and istream. The object std::cout is an output stream that outputs to the console.

I cannot run your code in its current state, but I suggest you do more research on operator overloading.

robotsfoundme
  • 418
  • 4
  • 18