Reputation: 6745
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];
}
Upvotes: 0
Views: 1228
Reputation: 98338
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.
Upvotes: 6