I'm trying to use std::max_element to find the largest element in a std::forward_list of a defined structure. Here is the code below:
//.h file:
#include <unordered_map>
#include <forward_list>
// my structure:
struct A
{
uint8_t length;
bool reverseMatch;
bool operator<(const A& rhs);
A() : length(0), reverseMatch(false) {}
};
using Alias = std::unordered_map<uint32_t, std::forward_list<A>>;
class B
{
Alias data;
public:
parse(string inputFilename);
A getBestMatch(uint32_t lineNumber);
};
Troublesome function:
//.cpp file
#include <sstream>
#include <algorithm>
#include <string>
#include "file.h"
bool A::operator<(const MUMmatch& rhs)
{
return (this->length < rhs.length);
}
A B::getBestMatch(uint32_t lineNumber)
{
A ret;
auto dataIter = this->data.find(lineNumber);
if (dataIter != data.end())
{
ret = *(std::max_element(dataIter->second.begin(), dataIter->second.end()));//!!!ERROR!!!
}
return ret;
}
The error that I'm getting is "Invalid operands to binary expression ('const A' and 'const A')". I'm not sure why my operator< overload is having issues here. Do I need to define other operands as well? If so, why? All documentation that I have read states that std::max_element uses the < operator. Thanks!
Aucun commentaire:
Enregistrer un commentaire