不断获得“不匹配”操作员< < C++ >
我正在编写一个程序来读取文件中的文本,我需要打印出文件中每个数字出现的位置。例如,.txt文件如下所示:不断获得“不匹配”操作员< < C++ >,c++,C++,我正在编写一个程序来读取文件中的文本,我需要打印出文件中每个数字出现的位置。例如,.txt文件如下所示: one two one two three three two one 我的输出应该如下所示: one: 0, 2, 7 two: 1, 3, 6 three: 4, 5 一切都很好,直到我尝试显示字符串类型,ListIt,然后我得到了“操作符< P>”的完全不匹配。问题是当你尝试输出*I.StIn时,因为我是My::Idter的类型。第二个是一个列表,C++不知道如何输出它。 C++
one
two one two
three three two one
我的输出应该如下所示:
one: 0, 2, 7
two: 1, 3, 6
three: 4, 5
一切都很好,直到我尝试显示字符串类型,ListIt,然后我得到了“操作符< P>”的完全不匹配。问题是当你尝试输出*I.StIn时,因为我是My::Idter的类型。第二个是一个列表,C++不知道如何输出它。
C++,你有两种选择——要么重载OSoS&运算符< P>问题是当你尝试输出*I.秒时,因为我是类型映射::迭代器*.I.是一个列表,C++不知道如何输出它。 您有两种选择-重载ostream&operator最简单的解决方案:
// !!! DO NOT DO THIS AT HOME OR AT ALL !!!
namespace std {
template <typename T, typename A>
ostream& operator<<(ostream& out, list<T, A> const& l) {
if (l.empty()) { return out << "[]"; }
out << '[';
bool first = true;
for (auto const& t: l) {
if (first) { first = false; } else { out << ", "; }
out << t;
}
return out << ']';
} // operator<<
} // namespace std
注意:除了创建自定义流之外,还有其他解决方案,例如复制粘贴std::copyexample.begin、example.end、std::ostream_iteratorstd::cout;在任何需要打印列表的地方,我都会遇到另一个方便的解决方案。最简单的解决方案:
// !!! DO NOT DO THIS AT HOME OR AT ALL !!!
namespace std {
template <typename T, typename A>
ostream& operator<<(ostream& out, list<T, A> const& l) {
if (l.empty()) { return out << "[]"; }
out << '[';
bool first = true;
for (auto const& t: l) {
if (first) { first = false; } else { out << ", "; }
out << t;
}
return out << ']';
} // operator<<
} // namespace std
注意:除了创建自定义流之外,还有其他解决方案,例如复制粘贴std::copyexample.begin、example.end、std::ostream_iteratorstd::cout;在任何地方都需要打印列表,但我还没有遇到另一个方便的列表。当询问编译错误时:1.发布完整且未编辑的错误消息,除非是雀巢的页面d模板错误,在这种情况下发布前几行和后几行,2.指出错误发生的源代码行。如果该行是复杂的表达式,请将其分解为多个语句,再次编译,然后在此处发布。当询问编译错误时:1.发布完整且未编辑的错误消息,除非是n页ested模板错误,在这种情况下发布前几行和后几行,2.指出错误发生在哪个源代码行上。如果该行是一个复杂的表达式,请将其分解为多个语句,再次编译,然后发布到此处。噢,duh,以前应该看到过。谢谢!+1;另外,第三个选项是使用std::copyi->second.begin,i->second.end,std::ostream_iteratorstd::cout;就我个人而言,我一直认为所有标准类型都没有自动打印选项是一个缺陷operator@MatthieuM我不明白最烦人的问题是std::ostream&operator@RonakPatel:ADL参数相关外观up将在引用的参数的名称空间中查找函数。当您使用std::cout Oh duh时,应该已经看到了。谢谢!+1;另外,第三个选项是使用std::copyi->second.begin,i->second.end,std::ostream_iteratorstd::cout;就个人而言,我一直认为这是一个没有自动打印选项的缺陷所有标准类型。最烦人的问题是std::ostream&operator@MatthieuM我不明白最烦人的问题是std::ostream&operator@RonakPatel:ADL参数相关查找将在引用参数的名称空间中查找函数。使用std::cout时
namespace project {
class OStream {
public:
explicit OStream(std::ostream& out): _out(out) {}
template <typename T>
OStream& operator<<(T const& t) { print(*this, t); return *this; }
template <typename T>
void push(T const& t) { _out << t; }
private:
std::ostream& _out;
}; // class OStream
// Generic Operator (directly forwards to `std::ostream`)
template <typename T>
void print(OStream& out, T const& t) { out.push(t); }
// STL Containers
template <typename It>
void print_range(OStream& out, It begin, It end) {
if (begin == end) { out << "[]"; return; }
out << '[' << *begin;
for (++begin; begin != end; ++begin) { out << ", " << *begin; }
out << ']';
} // push_range
template <typename T, typename A>
void print(OStream& out, std::list<T, A> const& l) {
print_range(out, l.begin(), l.end());
} // operator<<
} // namespace project
// usage
int main() {
std::list<int> example = { 1, 2, 3, 4 };
project::OStream(std::cout) << example << "\n";
}