重载一对 <std::string, std::Set 的<<运算符>
Overloading the << operator for a Pair of <std::string, std::Set>
我正在为我的CS课做一个实验室,我们必须制作口袋妖怪的哈希图,口袋妖怪的动作,口袋妖怪效果和口袋妖怪无效。有效性和无效性的哈希图属于Pair<Key, Value>
类型,其中Key
是std::string
,Value
是Set<std::string>
。当我尝试使用toString()
函数输出哈希图时,我得到以下结果:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type '_Ty2' (or there is no acceptable conversion) Lab 9 c:usersmaxosonedrive_devlab 9lab 9hashmap.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 对象。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- C++运算符<<调用::ostream而不是std::osttream
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++重载<<具有typedef'd std::vector
- 以x的倍数填充前导零,使用std::cout<<std::十六进制
- 错误:没有匹配'运算符<<"在'std::cout
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 将std::endl传递给std::operator<<
- std::映射<>或std::vector<>在处理大型标志集时
- 重载& lt; & lt;使用命名空间std