打印出对象矢量 [英] Print Out Vector of Objects
本文介绍了打印出对象矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试打印出对象的矢量但是当我使用cout<<它给出了如下所示的错误。请提供有关如何迭代和打印存储在对象中的信息的建议。
I am trying to print out the vector of objects but when I used the cout<< it gives error as shown below. Please provide advise on how to iterate and print out the information stored in the objects.
#include <iostream>
#include <vector>
#include <list>
#include<fstream>
using namespace std;
class Date
{
public:
int day;
int month;
int year;
Date(int day, int month, int year): day(day), month(month), year(year) {};
};
int main ()
{
vector <Date> date;
date.push_back(Date(2, 3, 1980));
date.push_back(Date(5, 4, 1982));
date.push_back(Date(1, 6, 1989));
for(auto it = date.begin(); it != date.end(); it++)
{
cout<<*it<<endl; //Get error message
}
return 0;
}
推荐答案
嗯,当然。这是因为您没有为Data
定义运算符''<<''。您可以按组件输出数据,也可以根据每个组件的输出定义运算符。
这篇Microsoft文章对此进行了解释: http://msdn.microsoft.com/en-us/library/1z2f6c2k%28v=vs。 110%29.aspx [ ^ ]。
-SA
Well, of course. This is because you did not define the operator ''<<'' forData
. You can output the data per-component or define the operator based on such per-component output.
This is explained in this Microsoft article: http://msdn.microsoft.com/en-us/library/1z2f6c2k%28v=vs.110%29.aspx[^].
—SA
这篇关于打印出对象矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文