I am getting error "no match for operator==in__first.
Here is the code:
header file:
#include
#include
#include
#include
#include
using namespace std;
struct rankingElement {
string url;
int rank;
};
class RankingCreator {
public:
static const int MAX_QUERY_SIZE = 20;
RankingCreator();
virtual ~RankingCreator();
bool checkPageRank(rankingElement rElement, vector ranking);
void insertIntoRanking(rankingElement rElement);
};
And source file :
#include "RankingCreator.h"
bool RankingCreator::checkPageRank(rankingElement rElement,
vector ranking)
{
if (ranking.size() < MAX_QUERY_SIZE) {
// enough space for another page in ranking
return true;
} else {
if (find(ranking.begin(), ranking.end(), rElement.url) != ranking.end()) {
// url is already in ranking
return false;
} else {
}
}
return true;
}
I tried commenting some blocks of code in source file and the line with find() function seems to generate the errors. It is function from class algorithm used to check whether a vector already contains certain element. I found out that it is wrong, because I try to compare struct with string in find function. I can handle that by copying urls from ranking to another vector of string and then use that vector of strings in find function, but when I tried to do that I can't access elements in 'ranking' vector - for instance ranking[i].url doesn't work and I don't know why.. and help would be appreciated.
Answer
find(ranking.begin(), ranking.end(), rElement.url)
you are telling find to search for rElement.url
(which is of type string
) in [ranking.begin(), ranking.end())
(which has elements of type rankingElements
). When not explicitely giving std::find
a comparison function it tries to use operator==
to compare the elements with the searched for elements. So it tries to invoke operator==(rankingElement, string)
which obviously doesn't exist. Imo the best way to make something like that work is to use find_if
which accepts a predicate:
struct functor {
string url;
functor(const string& str):url(str){}
bool operator()(const rankingElement& val) { return val.url == this->url; }
};
...
if(find_if(ranking.begin(), ranking.end(), functor(rElement.url)) != ranking.end() )
otherwise you can write an operator==
:
bool operator==(const rankingElement& elem, const string& url)
{ return elem.url == url; }
this will work with find
No comments:
Post a Comment