c++ - 为 Pair 重载 << 运算符
问题描述
我正在为我的 CS 课程建立一个实验室,我们必须制作 Pokemon、Pokemon 动作、Pokemon 有效和 Pokemon 无效的 HashMap。有效性和无效性的 HashMap 的类型为Pair<Key, Value>
where Key
is astd::string
和Value
is a Set<std::string>
。当我尝试使用我的toString()
函数输出 HashMap 时,我得到以下信息:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type '_Ty2' (or there is no acceptable conversion) Lab 9 c:\users\maxos\onedrive\_dev\lab 9\lab 9\hashmap.h 100
我认为这与不知道如何在 上使用<<
有关Set<std::string>
,但我<<
在类中重载了运算符。有任何想法吗?谢谢!
我尝试在实际Set
类以及基础LinkedList
类中重载运算符,但没有任何效果。
我的toString()
功能:
virtual std::string toString() const
{
std::stringstream outputStream;
outputStream << currSize << "/" << capacity << std::endl;
for (size_t i = 0; i < capacity; i++)
{
if (myPairs[i].first.length())
{
outputStream << " [" << i << ":" <<
myPairs[i].first << "->" << myPairs[i].second << "]" << std::endl;
}
}
return outputStream.str();
}
我的运算符重载
friend std::ostream & operator<<(std::ostream & os, const ModLinkedList<T>& list)
{
os << list.toString();
return os;
}
我只需要正确输出数据,但我不确定我需要重载什么来修复这个错误。
解决方案
我有很多反对票,如果我的问题问得不好,很抱歉,这是我的第一个:P。不过,我发现了我的问题,我只是愚蠢并试图将 LinkedList 对象传递给 << 重载而不是 Set 对象。
推荐阅读
- ruby-on-rails - 自定义 Ruby 模块的 LoadError
- java - 为什么 Completable 和 Observable 的订阅时间副作用调度不同?
- angular - 从多个 Observable 获取结果
- Angular 5 路线守卫 - windows - 如何在没有 fork(2) 的系统中使用 git?
- sparql - Blazegraph bigdata.war 2.1.5 允许来自“外部”的 SPARQL SERVICE
- sql - 仅获取前 10 行
- c# - 向 ASP.NET Identity 中的 ApplicationUser 类添加关系(数据库优先)
- javascript - 调整高度 tinyMCE 编辑器
- node.js - 访问 handlebars.js 中的下一个数组项
- c# - 异常将局部视图呈现为包含另一个局部视图的字符串