Edit, Update: Great answers thank you all. I've written operator overloading for < and == operators. I am no longer receiving the error. I also correctly passed the vector by reference instead of by value into my removeDup(vector<Graph::Vertex>&);
function.
I have a vector of objects that I need to remove duplicates from, but I am receiving an error that I am unfamiliar with (the error is below). It's a very simple object publicly contained within a class called Graph
:
struct Vertex
{
string name;
int num;
public:
Vertex();
Vertex(string, int);
void printVertex();
};
I have read through a few threads regarding this (This one in particular) but I am getting a very strange error when I try to sort my vector using unique.
I add to the vector using push_back
:
vector<Graph::Vertex> v;
Graph::Vertex first(string1, count);
v.push_back(first);
and attempt to sort using this code that I found in another thread:
void removeDup(vector<Graph::Vertex> v)
{
sort( v.begin(), v.end());
v.erase( unique( v.begin(), v.end() ), v.end() );
}
However, when I use this function, I get an error:
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Graph::Vertex' (or there is no acceptable conversion)
I am slightly new to programming in C++ and have no idea why this error is happening, or what it means. Could someone explain why I am getting this error? Do I need to write an operator overload function to take care of it?
Tried very hard to google (and use this website) for the solution to this error, but in the end had to make my first post here for help. A heads up: this is for a homework assignment, so I decided not to include the rest of my code to avoid being spoon fed the answers, but if I haven't given enough information, please let me know.