简体   繁体   English

列出迭代器错误C ++

[英]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. 它在for循环中的cout之后加了“ <<”。

#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 错误2错误C2679:二进制'<<':未找到采用'weatherStation'类型的右侧操作数的运算符(或没有可接受的转换)\\ zorak2 \\ users $ \\ s0941625 \\ mydocuments \\ 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 3 IntelliSense:没有运算符“ <<”与这些操作数匹配,操作数类型为: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 . 需要由std::cout One option is to define the corresponding stream operator as a friend inside your class: 一种选择是将相应的流运算符定义为类中的friend

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++. 显示问题是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. 将来您可以做的是定义一个抽象类,我们将其称为IDisplay ,它声明一个纯虚函数std::ostream& display(std::ostream&) const并声明operator<<作为朋友。 Then every class that you want to be display-able must inherit from IDisplay and consequently implement the display member function. 然后,每个希望可显示的类都必须继承自IDisplay并因此实现display成员函数。 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 住在科利鲁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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