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];
}
12
  • 2
    Instead of describing what you think the implementation is - post the actual implementation. It obviously is not what you think it is. Commented Oct 3, 2011 at 9: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. Commented Oct 3, 2011 at 9:15
  • 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). Commented Oct 3, 2011 at 9:20
  • 1
    So why on Earth is it qualified with std::? See stackoverflow.com/q/320798/78845 Commented Oct 3, 2011 at 9:21
  • 2
    Can you post the implementation of operator[]? Commented Oct 3, 2011 at 9:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, but then I cannot use iterators in the way that I have in my implementation. How could I fix that?
Using map<string, string>::const_iterator attrib;? You are not modifying the object, are you?
Aha! That was it. I didn't know about the const_iterator. Thanks!
Oh sorry, a jumped the gun a little bit. I'm actually still getting the error :(
Ah, but now it is fixed. Making the operator[] return a const string was the final touch.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.