binary'==':找不到带有'std :: string'类型的左手操作数的运算符(或者没有可接受的转换)

时间:2012-03-04 22:31:15

标签: c++ templates const

我正在编写一个比较std :: strings的模板类函数。 std :: string是模板参数。我的问题是我无法将两个const字符串与“==”运算符进行比较,那么我认为我创建了两个非const临时字符串变量来进行比较,但它仍然无法编译。不知道为什么。

类VGraph实例为VGraph<std::string, std::string> myGraph;

template <typename V, typename E>
size_t VGraph<V, E>::find(const V& vert)
{
    V temp = vert; // (1)
    for (size_t i=0; i<graph.size(); i++)
    {
        V noneConst = graph[i].getVertex(); // (2)
        if (temp==noneConst)// I think broblem is here, and tried to fix using (1)(2)
            return i;
    }
    return graph.size();
}

相关功能的原型

template <typename V, typename E>
const V& VVertex<V, E>::getVertex();

1 个答案:

答案 0 :(得分:18)

你可能忘记了一个明确的:

#include <string>

std::string类由您包含的另一个标头定义,但不是运营商==