1
votes

In this function I would like to search 'vertex' in the vector queue.

bool PriorityQueue::contains(VertexPriority vertex) const {
     return (std::find(queue.begin(), queue.end(), vertex) != queue.end());
}

The vector queue is an instance of this object:

std::vector<VertexPriority> queue;

And my operator overloading is this:

bool operator==(const VertexPriority& v){ return (v.vertex == vertex); }

How can I solve this error?

The errors risen are the next ones, and at the beginning of every error there is the following path:

C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h

In function `_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = VertexPriority]':

instantiated from `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = __gnu_cxx::__normal_iterator > >, _Tp = VertexPriority]'

passing const VertexPriority' asthis' argument of `bool VertexPriority::operator==(const VertexPriority&)' discards qualifiers

1
no match for 'operator==' Did you implement it in VertexPriority>Retired Ninja
Are you finding a vertex in an edge list?!masoud
no sorry, I wrote wrong the text of this question, now I'll editgiacomotb
I implemented the operator overloading just now and edited the questiongiacomotb

1 Answers

2
votes

Make the operator const:

bool operator==(const VertexPriority& v) const 
{ 
    return (v.vertex == vertex); 
}

Note that this makes the member function const itself, which enables the compiler to invoke it on a const object of VertexPriority type.

This is actually precisely what the compiler sais:

passing const VertexPriority' as this' argument of `bool VertexPriority::operator==(const VertexPriority&)' discards qualifiers

The VertexPriority object that is const, is being passed as the implicit thisargument to member functions.