简体   繁体   中英

list iterator error c++

I have the following code : im not sure what the issue is. It is underlining the ' <<' after the cout in the for loop.

#include <fstream>
#include <sstream>
#include <ostream>
#include <istream>
#include <string>
#include <iostream>

#include <iterator>
#include <list>

list<weatherStation> station;
weatherStation *aStation;

aStation = new weatherStation();

for (list<weatherStation>::iterator it = station.begin(); it != station.end(); ++it)
        {
            cout << *it << endl;
        }

The errors i'm getting are:

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'weatherStation' (or there is no acceptable conversion) \\zorak2\\users$\\s0941625\\my documents\\visual studio 2013\\projects\\lentzis\\lentzis\\newmain.cpp 100 1 Project1

and

3 IntelliSense: no operator "<<" matches these operands operand types are: std::ostream << weatherStation \\zorak2\\users$\\s0941625\\My Documents\\Visual Studio 2013\\Projects\\lentzis\\lentzis\\newMain.cpp 101 10 Project1

Short answer

weatherStation 

needs to be display-able by std::cout . One option is to define the corresponding stream operator as a friend inside your class:

inline friend 
std::ostream& operator<<(std::ostream& os, const weatherStation& ws)
{
    os << weatherStation.some_member; // you output it
    return os;
}

Long answer

The display issue is a recurring problem in C++. What you can do in the future is define an abstract class, we'll call it IDisplay , which declares a pure virtual function std::ostream& display(std::ostream&) const and declares operator<< as a friend. Then every class that you want to be display-able must inherit from IDisplay and consequently implement the display member function. This approach reuses the code and is pretty elegant. Example below:

#include <iostream>

class IDisplay
{
private:
    /**
    * \brief Must be overridden by all derived classes
    *
    * The actual stream extraction processing is performed by the overriden
    * member function in the derived class. This function is automatically
    * invoked by friend inline std::ostream& operator<<(std::ostream& os,
    * const IDisplay& rhs).
    */
    virtual std::ostream& display(std::ostream& os) const = 0;

public:
    /**
    * \brief Default virtual destructor
    */
    virtual ~IDisplay() = default;

    /**
    * \brief Overloads the extraction operator
    *
    * Delegates the work to the virtual function IDisplay::display()
    */
    friend inline
    std::ostream& operator<<(std::ostream& os, const IDisplay& rhs)
    {
        return rhs.display(os);
    }
}; /* class IDisplay */

class Foo: public IDisplay
{
public:
    std::ostream& display(std::ostream& os) const override 
    {
        return os << "Foo";
    }
};

class Bar: public IDisplay
{
public:
    std::ostream& display(std::ostream& os) const override 
    {
        return os << "Bar";
    }
};

int main() 
{
    Foo foo;
    Bar bar;
    std::cout << foo << " " << bar;    
}

Live on Coliru

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