Click here to Skip to main content
16,004,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#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");

}
Posted
Updated 8-Nov-14 12:08pm
v2

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:

C++
if (thefruit[1] == thefruit[2])
{
  // whatever
}

It is going to throw the same error but with no 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[^]
 
Share this answer
 
v3
I think this explains and answers your question better than I could

http://stackoverflow.com/questions/16513046/error-no-operator-matches-these-operands[^]

'g'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900