Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid operands to binary expression ('const' and 'const' ) [duplicate]

Tags:

c++

I am a C++ noob, so please don’t mind if you find this question silly

I am declaring map in C++ a below:

std::map<CartesianLocation, std::list<RadioSignal<RadioDevice>>> radioMap;

Full code:

Don't know but using below code, I could able to solve my issue

class RadioMap:public std::iterator_traits<CartesianLocation>, public Cloneable<RadioMap> {
private:
    std::map<const CartesianLocation*, const std::list<RadioSignal<RadioDevice>>*> radioMap;
    std::vector<RadioDevice> radioDevices;

public:
    void add(const CartesianLocation *location, const std::list<RadioSignal<RadioDevice>> *observedSignals){
        radioMap[location] = observedSignals;
}

On this line radioMap[location] = observedSignals; I am ended with this error

“Invalid operands to binary expression ('const CartesianLocation' and 'const CartesianLocation’)” on struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool> { _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const {return __x < __y;} };

Any idea where I may be wrong?

like image 206
Tarun Avatar asked May 18 '16 13:05

Tarun


1 Answers

You are not providing a comparator for std::map, and so it uses std::less. But std::less doesn't have an overload for CartesianLocation (and CartesianLocation doesn't have an operator<), so you get an error.

You can add an operator<:

struct CartesianLocation
{
    //Other stuff
    bool operator<(const CartesianLocation& loc2) const
    {
        //Return true if this is less than loc2
    }
};

Another way is to define a custom comparator, for example:

struct comp
{
    bool operator()(const CartesianLocation& loc1, const CartesianLocation& loc2) const
    {
        //Compare the 2 locations, return true if loc1 is less than loc2
    }
};

Then you can pass it to std::map:

std::map<CartesianLocation, std::list<RadioSignal<RadioDevice>>, comp> radioMap;
like image 50
Rakete1111 Avatar answered Sep 28 '22 03:09

Rakete1111