3

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

第一个 for 循环给了我一条错误消息:

NO BINARY OPERATOR FOUND << 无法转换

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

MyClass.h:

class MyClass{

private:

    string newTodayTaskString;

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

    ~MyClass(){}
};

这个 for 循环遍历字符串向量并完美运行。为什么?

vector<string>stringVector;
   for (vector<string>::iterator i = stringVector.begin(); 
                         i != stringVector.end(); 
                         ++i) 
            {
                cout<<*i<<endl;
            }
4

5 回答 5

5

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

std::string s = "Hello";
std::cout << s;

但不是

MyClass o("Hello");
std::cout << o;      

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

于 2012-07-04T08:56:01.953 回答
3

如果您希望能够直接调用std::cout::operator <<.

您可以将其定义为:

 std::ostream& operator << (std::ostream& stream, const MyClass& obj)
 {
    stream << obj.newTodayTaskString;
 }

并将此运算符声明为朋友,以便它可以访问类的私有成员或为您的类提供打印函数并使用它。

于 2012-07-04T08:54:13.823 回答
1

我认为您想打印班级的成员,而不是班级本身。

例如:

cout << (*i).Name << endl; 
于 2012-07-04T08:54:55.737 回答
1

您正在遍历“MyClass”的向量,这可能是用户定义的类型。因此,您必须告诉编译器,您对“MyClass”类的哪些数据感兴趣。下面是示例代码 - 仅用于理解目的

// Temp_Practice.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<memory>
#include<ostream>
#include <vector>>
using namespace std;

// This example is just for understanding/Demo purpose.
// it will not run on any compiler
class MyClass {
public:
    std::string _name;
    std::string _address;
    int         _age;
    // Constructor
public:
    MyClass() {}
    MyClass(std::string name, std::string address, int age) :_name(name), _address(address), _age(age) {}

    //Destructor
    //..

    // other stuff
    friend ostream& operator << (ostream& os, std::string _input);

};
 ostream& operator << (ostream& os, std::string _input)
{
     os << _input.c_str();
     return os;
}

int main()
{
    std::vector<MyClass> vecMyClass;

    MyClass temp("AA", "BB", 1);
    vecMyClass.push_back(temp);

    MyClass temp1("CC", "DD", 2);
    vecMyClass.push_back(temp1);

    MyClass temp2("EE", "FF", 3);
    vecMyClass.push_back(temp2);

    MyClass temp3("GG", "HH", 4);
    vecMyClass.push_back(temp3);

    MyClass temp4("II", "JJ", 5);
    vecMyClass.push_back(temp4);

    MyClass temp5("KK", "LL", 6);
    vecMyClass.push_back(temp5);
    std::vector<MyClass>::iterator itr;
    for ( itr = vecMyClass.begin(); itr != vecMyClass.end(); ++itr)
    {
        // Compiler throws error; it does not know what programer wants to print. So its 
        // programer responsiblity to let compiler know what to be printed
         //std::cout << itr << std::endl; //  Error!!!!

        // Correct Code
        std::cout << itr->_name << itr->_address << itr->_age << std::endl;
    }
    return 0;
}
于 2018-01-27T04:35:49.860 回答
0

您应该重载输出运算符。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

class MyClass{

private:
    std::string newTodayTaskString;

public:
    explicit MyClass(const std::string t) : newTodayTaskString (t){}
    std::ostream& print(std::ostream& os) const { return os << newTodayTaskString; }

    ~MyClass(){}
};

std::ostream& operator << (std::ostream& os, const MyClass& obj)
{
    return obj.print(os);
}

int main()
{
    std::vector<MyClass> vec = {MyClass("add"), MyClass("clear")};
    std::copy(vec.begin(), vec.end(), std::ostream_iterator<MyClass>(std::cout, "\n"));
}
于 2012-07-04T08:58:04.603 回答