ERROR: no match for 'operator<<" in 'std::cout

前端 未结 5 1256
孤城傲影
孤城傲影 2021-01-22 13:32

I realize this error is usually due to some syntax or type issues but I am not sure how to solve this problem. I think it may do with the type of findRt.

vector&         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 14:04

    Why the error?
    The compiler reports an error because there is no overloaded version of << operator to handle the type vector which your function findRightTriangles() returns.
    << is overloaded only for most of the built-in data types and not for custom classes.

    How to output the vector using cout?
    There are two ways:

    Solution 1:
    A two step procedure:

    Step1: You will have to iterate through the vector and cout each contained triangle.

    std::vector::const_iterator iter= vec.begin();
    for(iter; iter != vec.end(); ++iter)
    {
        cout<<*iter; //This is what step 2 provides for
    }
    

    Step 2: You will have to overload << for the triangle class as well.

    ostream& operator<<( ostream& os, const triangle &) {}
    

    Solution 2:
    One step Solution.
    Alternately, You can overload << for vector type itself:

    ostream& operator<<( ostream& os, const vector&)
    {
    
    }
    

    I would personally prefer the Solution 1. It is more readable & Usually more often than not one would already have an overloaded << for the vector type being used and it could be leveraged.

提交回复
热议问题