9

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?

k-a-v
  • 326
  • 5
  • 22
Tarun
  • 329
  • 1
  • 4
  • 16
  • 7
    I think because you have user defined types specifically `CatersianLocation` and whatever `RadioSignal` and `RadioDevice` is they need to define the less than operator `<` for the comparator in the `map` to order the elements correctly, see related: http://stackoverflow.com/questions/1102392/stdmaps-with-user-defined-types-as-key or you can define a comparator functor as suggested in the linked question – EdChum May 18 '16 at 13:34

1 Answers1

7

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;
Rakete1111
  • 47,013
  • 16
  • 123
  • 162