获取向量元素的输出有问题吗? [英] Problems with get the output of the elements of a vector?
问题描述
我今天正在使用矢量和课程。我尝试做一些不同的事情,在某种程度上我成功了但是我有这个问题得到最终输出。这就是我所做的 - 我创建了一个类水果然后我创建了一个水果矢量(数据类型)并开始在其中推送不同类型的水果对象。它起作用,完全没有问题。但是,当我想获得向量(水果)中的元素的输出时,我只是无法使用正常方式我得到向量元素输出并且它一直给我错误。这是我的代码 - 请有人建议我在哪里犯了错误 - **我没有在这里包含我的水果类代码
#include < iostream >
#include < span class =code-preprocessor> < vector >
#include fruit.h
使用 命名空间标准;
int main()
{
fruit apple; // 这是我的水果对象苹果的水果类
fruit cherry;
vector< fruit>水果;
thefruit.push_back(apple);
thefruit.push_back(cherry);
for ( int i = 0 ; i< thefruit.size(); i ++)
{
cout<< 水果向量中的水果是:<< thefruit [i]<< endl; // 这就是我//得到错误的地方 - 错误是no operator<<匹配这个操作数//
}
system( pause);
}
我认为这比我更好地解释和回答你的问题
http://stackoverflow.com/questions/16513046/error- no-operator-matches-these-operands [ ^ ]
'g'
既然你没有编写fruits类的代码,我们就无法100%肯定。但是这个错误很好地解释了这个问题。
如果你不重载<<
班级内的操作员。
与平等,比较和其他操作员相同。
如果您尝试这样做:
if (thefruit [ 1 ] == thefruit [ 2 ])
{
// 无论
}
它将抛出相同的错误,但是没有运算符==
而是
查看以下链接:
http:// www .tutorialspoint.com / cplusplus / cpp_overloading.htm [ ^ ]
http://en.cppreference.com/w/ cpp / language / operators [ ^ ]
I was working with vectors and classes today. I tried to do something different and to some extent i was successful but then i have this problem getting the final output. This is what i did- I created a class fruit and then i created a vector of fruit(data-type) and started pushing different types of fruit objects in it. It worked and there was no problem at all. But when i wanted to get the output of the elements in the vector (fruits), i just couldn't use the normal way i get the vector elements output and it kept giving me error. This is my code- Please can someone suggest me where i made the mistake- **I haven't include my fruit class code here
#include <iostream>
#include <vector>
#include "fruit.h"
using namespace std;
int main()
{
fruit apple; // this is my fruit class with my fruit object apple
fruit cherry;
vector<fruit> thefruit;
thefruit.push_back(apple);
thefruit.push_back(cherry);
for(int i=0; i<thefruit.size(); i++)
{
cout<<"The fruits in the fruit vector are:"<<thefruit[i]<<endl;//this is where i //get the error- The error is" no operator "<<" matches this operands//
}
system("pause");
}
I think this explains and answers your question better than I could
http://stackoverflow.com/questions/16513046/error-no-operator-matches-these-operands[^]
'g'
Since you didn't wrote the code of the "fruits" class, we can not be 100% sure. But the error is quite explaining the problem.
You can not print out a class if you don't overload the<<
operator within the class.
It is the same as with equality, comparison and other operators.
If you try to do:
if (thefruit[1] == thefruit[2]) { // whatever }
It is going to throw the same error but withno operator "=="
instead
[edit]
Have a look to following links:
http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm[^]
http://en.cppreference.com/w/cpp/language/operators[^]
这篇关于获取向量元素的输出有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!