Dan*_*iel 0 c++ compiler-errors compilation stdio
我以前输错了错误信息.现在修好了.
我目前收到以下编译器错误消息
error: no match for 'operator<<' in 'std::cout << Collection::operator[](int)(j)'
Run Code Online (Sandbox Code Playgroud)
编译器抱怨的代码是
cout << testingSet[j];
Run Code Online (Sandbox Code Playgroud)
哪个testingSet类型的对象Collection已operator[]重载以返回类型的对象Example. Example有一个operator<<为ostream和Example 重载的友元函数.
注意:这实际上在Visual Studio中编译得很好; 但是不使用g ++编译.
这是执行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;
}
Run Code Online (Sandbox Code Playgroud)
而且 operator[]
Example Collection::operator[](int i)
{
return examples[i];
}
Run Code Online (Sandbox Code Playgroud)
可能您的运营商应声明为:
ostream& operator<<(ostream &strm, const Example &ex)
Run Code Online (Sandbox Code Playgroud)
注意const-reference to Example.Visual Studio有一个扩展,允许绑定对非const r值的引用.我的猜测是你operator[]返回一个r值.
无论如何,operator<<应该是const因为不希望修改书面对象.
| 归档时间: |
|
| 查看次数: |
367 次 |
| 最近记录: |