为一对<std :: string,std :: set =“”>重载<<操作符

时间:2019-04-05 16:48:19

标签: c++

我正在为我的CS类进行实验室研究,我们必须制作Pokemon,Pokemon移动,Pokemon效果和Pokemon效果不佳的HashMap。有效性和无效性的HashMap的类型为Pair<Key, Value>,其中Keystd::string,而ValueSet<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;
}

我只需要正确输出数据,但是我不确定需要重载来解决此错误。

1 个答案:

答案 0 :(得分:0)

我有很多赞成票,很抱歉,如果我的问题提出得不好,这是我的第一个:P。不过,我发现了自己的问题,只是我很笨,试图将LinkedList对象传递给<<重载而不是Set对象。