【发布时间】:2011-10-03 09:11:36
【问题描述】:
我之前打错了错误信息。现已修复。
我目前收到以下编译器错误消息
error: no match for 'operator<<' in 'std::cout << Collection::operator[](int)(j)'
编译器抱怨的代码是
cout << testingSet[j];
其中testingSet 是Collection 类型的对象,它具有operator[] 重载以返回Example 类型的对象。 Example 有一个友元函数,它为 ostream 和 Example 重载 operator<<。
注意:这实际上在 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;
}
还有operator[]
Example Collection::operator[](int i)
{
return examples[i];
}
【问题讨论】:
-
不要描述你认为的实现是什么 - 发布实际的实现。显然不是你想的那样。
-
您能否为我们指明
std::Collection的文档方向?我以前没有遇到过 C++ 标准库的这一部分。 -
(另外,我会努力改掉写
using namespace std的习惯,改写std::ostream和std::cout的习惯)。 -
那么到底为什么它会被
std::限定呢?见stackoverflow.com/q/320798/78845 -
可以发一下
operator[]的实现吗?
标签: c++ compilation compiler-errors stdio