Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

I am writing a template class function comparing std::strings. std::string is the template parameter. My problem is that I can't compare two const string with "==" operator, then I think I create two non-const temporary string variables to performe the comparation, but it still can't compile. Don't know why.

class VGraph is instanced as 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();
}

Prototype of related function

template <typename V, typename E>
const V& VVertex<V, E>::getVertex();
like image 672
Howard Avatar asked Dec 01 '22 23:12

Howard


1 Answers

You probably forgot an explicit:

#include <string>

The std::string class is defined by a another header you included, but not the operator ==.

like image 83
alexisdm Avatar answered Dec 22 '22 06:12

alexisdm