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

前端 未结 5 1247
孤城傲影
孤城傲影 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 13:45

    You'll have to create an overloaded version of operator<< for std::cout. It would look something like the following:

    ostream& operator<<(ostream& out, const vector<triangle>& triangles);
    

    and at the end of the function, you simply do a return out; in order to return the std::ostream object out that was passed as the first argument (in your case that will be std::cout).

    In other words, when you do

    MyFoo object;
    std::cout << object;
    

    this is "syntactic sugar" for the following function call:

    MyFoo object;
    operator<<(std::cout, object);
    

    and would call a version of operator<< that looked like:

    ostream& operator<<(ostream& out, const MyFoo& my_object);
    

    If the above function was not defined, then you'd get an error like you're currently experiencing.

    0 讨论(0)
  • 2021-01-22 13:48

    Operator "<<" is not overloaded for the type triangle. Have you checked this link out?

    0 讨论(0)
  • 2021-01-22 13:50

    For cout to be able to output your object you have to tell it how. One way is to overload the << operator:

    ostream& operator<<( ostream& os, const vector<triangle>& vec ) {
        ...
        ... // here output to os the way you want it to
        os << ...
        return os;
    }
    
    0 讨论(0)
  • 2021-01-22 14:02

    Use an iterator http://www.cplusplus.com/reference/std/iterator/.

    Example would be

    vector<triangle>::iterator it;
    
    cout << "myvector contains:";
    for ( it=myvector.begin() ; it < myvector.end(); it++ )
        cout << " " << *it;
    
    cout << endl;
    
    return 0;
    

    This assumes you have an operator<< for the triangle type.

    0 讨论(0)
  • 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<triangle> 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<triangle>::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<triangle>&)
    {
    
    }
    

    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.

    0 讨论(0)
提交回复
热议问题