,c++"/>

首页 > 解决方案 > 为 Pair 重载 << 运算符

问题描述

我正在为我的 CS 课程建立一个实验室,我们必须制作 Pokemon、Pokemon 动作、Pokemon 有效和 Pokemon 无效的 HashMap。有效性和无效性的 HashMap 的类型为Pair<Key, Value>where Keyis astd::stringValueis 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;
}

我只需要正确输出数据,但我不确定我需要重载什么来修复这个错误。

标签: c++

解决方案


我有很多反对票,如果我的问题问得不好,很抱歉,这是我的第一个:P。不过,我发现了我的问题,我只是愚蠢并试图将 LinkedList 对象传递给 << 重载而不是 Set 对象。


推荐阅读