我正在写一个比较std :: strings的模板类函数。 std :: string是模板参数。我的问题是,我不能比较两个常量字符串与“==”运算符,然后我想我创建两个非常量临时字符串变量来执行比较,但它仍然无法编译。不知道为什么。二进制'==':没有找到运算符类型'std :: string'(或没有可接受的转换)的操作符
类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();
您使用的实例化中的V是什么? – PlasmaHH 2012-03-04 22:33:00
您可以比较两个常量字符串或两个常量引用字符串完全正常,您的问题必须在其他地方。 – cooky451 2012-03-04 22:35:07
@CaptainGiraffe:我假设错误消息是'binary'==':找不到操作符找到'std :: string'类型的左手操作数(或者没有可接受的转换)。否则,这个问题的标题将非常违反直觉。 – ruakh 2012-03-04 22:37:04