C++如何遍历对象的std :: vector并在控制台上显示内容

2 c++ stdvector

For循环应该遍历std :: vector并填充内容.

First for循环给出了一条错误消息:

没有找到二元运算符<<不可转换

vector<MyClass>classVector;
    for (vector<MyClass>::iterator i = classVector.begin();
                           i != classVector.end();
                           ++i)
            {
                cout << *i << endl;
            }
Run Code Online (Sandbox Code Playgroud)

MyClass.h:

class MyClass{

private:

    string newTodayTaskString;

public:
    MyClass(string t) : newTodayTaskString (t){}

    ~MyClass(){}
};
Run Code Online (Sandbox Code Playgroud)

这个for循环遍历字符串向量并且完美地工作.为什么?

vector<string>stringVector;
   for (vector<string>::iterator i = stringVector.begin(); 
                         i != stringVector.end(); 
                         ++i) 
            {
                cout<<*i<<endl;
            }
Run Code Online (Sandbox Code Playgroud)

Phi*_*ipp 5

这个问题与迭代无关,只是因为你可以写

std::string s = "Hello";
std::cout << s;
Run Code Online (Sandbox Code Playgroud)

但不是

MyClass o("Hello");
std::cout << o;      
Run Code Online (Sandbox Code Playgroud)

请参阅如何正确重载ostream的<<运算符?关于如何超载operator <<以使其工作!