0

I mistyped the error message before. It is fixed now.

I'm currently getting the following compiler error message

error: no match for 'operator<<' in 'std::cout << Collection::operator[](int)(j)'

The code that the compiler is complaining about is

cout << testingSet[j];

Where testingSet is an object of type Collection that has operator[] overloaded to return an object of type Example. Example has a friend function that overloads operator<< for ostream and Example.

note: this actually compiles just fine within Visual Studio; however does not compile using g++.

Here is the implementation of operator<<:

ostream& operator<<(ostream &strm, Example &ex)
{
     strm << endl << endl;
     strm << "{ ";
     map<string, string>::iterator attrib;
     for(attrib = ex.attributes.begin(); attrib != ex.attributes.end(); ++attrib)
     {
          strm << "(" << attrib->first << " = " << attrib->second << "), ";
     }
     return strm << "} classification = " << (ex.classification ? "true" : "false") << endl;
}

And the operator[]

Example Collection::operator[](int i)
{
      return examples[i];
}
Daniel
  • 6,595
  • 9
  • 38
  • 70
  • 2
    Instead of describing what you think the implementation is - post the actual implementation. It obviously is not what you think it is. – littleadv Oct 03 '11 at 09:14
  • Can you point us in the direction of the documentation for `std::Collection`? I've not come across this part of the C++ Standard Library before. – johnsyweb Oct 03 '11 at 09:15
  • Have you `#include` the file that contains the `Collection` definition? - it looks like the compiler can't find it (not even in the `std` namespace - which I guess you are `using`) – Tom Oct 03 '11 at 09:16
  • @johnsyweb: std::Collection is not part of the C++ Standard library. It is my own defined class. – Daniel Oct 03 '11 at 09:19
  • @Tom: Yes I have `#include` the file. It is strange because it compiles just fine in Visual Studio. – Daniel Oct 03 '11 at 09:20
  • 1
    (also, I would try to get out of the habit of writing `using namespace std` and into the habit of writing `std::ostream` and `std::cout`). – Tom Oct 03 '11 at 09:20
  • @littleadv: I have posted the implementation of the `operator<<`. Is that what you are looking for? – Daniel Oct 03 '11 at 09:20
  • 1
    So why on Earth is it qualified with `std::`? See http://stackoverflow.com/q/320798/78845 – johnsyweb Oct 03 '11 at 09:21
  • 2
    Can you post the implementation of `operator[]`? – interjay Oct 03 '11 at 09:25
  • @Johnsyweb: I realized that I accidentally miscopied the error message. Now it is fixed. – Daniel Oct 03 '11 at 09:26
  • @interjay: It just takes an int and returns the the `Example` stored at that index of a `vector` that is stored in the `Collection` object. – Daniel Oct 03 '11 at 09:28
  • @Daniel: I'm sure it does, but we still need to see it, particularly to see its signature. – interjay Oct 03 '11 at 09:30

1 Answers1

6

Probably your operator should be declared as:

ostream& operator<<(ostream &strm, const Example &ex)

Note the const-reference to Example. Visual Studio has an extension that allows to bind a reference to a non-const r-value. My guess is that your operator[] returns an r-value.

Anyway, an operator<< should be const as it is not expected to modify the written object.

rodrigo
  • 94,151
  • 12
  • 143
  • 190