简体   繁体   中英

C2679 binary '<<': no operator found which takes a right-hand operand of type

Template class of intervals, basically contains the edges of groups, in this instance its INT. The source code that requests the operator is

void testIntervalInt() {
    Interval<int> interval = Interval<int>(2, 1);
    cout << interval << endl;

    Interval<int> interval13(1, 3);
    Interval<int> interval24(2, 4);
    Interval<int> interval45(4, 5);
    cout << interval13 << endl;
    cout << interval24 << endl;
    cout << interval45 << endl;

    cout << "Does " << interval13 << " contain " << 2 << "? " << interval13.contains(2) << endl;
    cout << "Does " << interval24 << " contain " << 2 << "? " << interval24.contains(2) << endl;
    cout << "Does " << interval45 << " contain " << 2 << "? " << interval45.contains(2) << endl;

    cout << "Is " << interval13 << " before " << interval24 << "? "
        << interval13.isBefore(interval24) << endl;
    cout << "Is " << interval13 << " before " << interval45 << "? "
        << interval13.isBefore(interval45) << endl;
    cout << "Is " << interval24 << " before " << interval45 << "? "
        << interval24.isBefore(interval45) << endl;

    cout << "Is " << interval13 << " after " << interval24 << "? "
        << interval13.isAfter(interval24) << endl;
    cout << "Is " << interval13 << " after " << interval45 << "? "
        << interval13.isAfter(interval45) << endl;
    cout << "Is " << interval24 << " after " << interval45 << "? "
        << interval24.isAfter(interval45) << endl;

    cout << "Does " << interval13 << " intersect " << interval24 << "? "
        << interval13.intersects(interval24) << endl;
    cout << "Does " << interval13 << " intersect " << interval45 << "? "
        << interval13.intersects(interval45) << endl;
    cout << "Does " << interval24 << " intersect " << interval45 << "? "
        << interval24.intersects(interval45) << endl;

    cout << "Does " << interval24 << " intersect " << interval13 << "? "
        << interval24.intersects(interval13) << endl;
    cout << "Does " << interval45 << " intersect " << interval13 << "? "
        << interval45.intersects(interval13) << endl;
    cout << "Does " << interval45 << " intersect " << interval24 << "? "
        << interval45.intersects(interval24) << endl;

    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;


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

However the problem is not throughout the whole thing. All intervals up to the ones with && or ||are printed to the console without a problem. Here is the overload

friend ostream& operator<<(ostream& stream, Interval<T>& C) {
        bool error_flag;
        error_flag = C.isValid();
        if (error_flag == true) {
            stream << C.getProblem();
        }
        else {
            stream << "(" << C.getleft() << ", " << C.getright() << ")";
        }
        return stream;
    }

Isvalid returns an error flag and problem returns a string.

I assume the problem stems from the overloads of && and ||, however no matter what i try, it just wont work and keeps the same errors:

template<typename T>
Interval<T> Interval<T>::operator&&(const Interval<T>& other) {
    bool intersection_flag;
    intersection_flag = this->intersects(other);
    if (intersection_flag == false) {
        return Interval<int>(0, 0);
    }
    else {
        if (this->Left_edge > other.getright()) {
            return Interval<T>(other.getright(), this->Left_Edge);
        }
        else {
            return Interval<T>(other.getleft(), this->Right_Edge);
        }
    }
}

template<typename T>
Interval<T> Interval<T>::operator||(const Interval<T>& other) {
    bool intersection_flag;
    Interval<T> interval;
    intersection_flag = this->intersects(other);
    if (intersection_flag == false && this->Right_Edge != other.Left_Edge && this->Left_Edge != other.Right_Edge) {
        interval = Interval<int>(1, 0);
        return interval;
    }
    else {
        if (this->Left_Edge >= other.Right_Edge) {
            interval = Interval<T>(other.Left_Edge, this->Right_Edge);
            return interval;
        }
        else {
            interval = Interval<T>(this->Left_Edge, other.Right_Edge);
            return interval;
        }
    }
}

Does anyone have a clue as to why i keep getting Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'Interval' (or there is no acceptable conversion)?

Your operator<< needs to take the second parameter as a const reference:

friend ostream& operator<<(ostream& stream, const Interval<T>& C)

without the const, a temporary object (as returned by operator&& ) cannot be bound to the reference to pass to the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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