重载一对 <std::string, std::Set 的<<运算符>

Overloading the << operator for a Pair of <std::string, std::Set>

本文关键字:lt std 运算符 gt Set 重载 string      更新时间:2023-10-16

我正在为我的CS课做一个实验室,我们必须制作口袋妖怪的哈希图,口袋妖怪的动作,口袋妖怪效果和口袋妖怪无效。有效性和无效性的哈希图属于Pair<Key, Value>类型,其中Keystd::stringValueSet<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 对象。