二进制表达式的操作数无效('const'和'const')

Tar*_*run 5 c++

我是C++菜鸟,所以如果你觉得这个问题很傻,请不要介意

我在C++中声明地图如下:

std::map<CartesianLocation, std::list<RadioSignal<RadioDevice>>> radioMap;
Run Code Online (Sandbox Code Playgroud)

完整代码:

不知道但是使用下面的代码,我能够解决我的问题类RadioMap:public std :: iterator_traits,public Cloneable {private:

std::map<const CartesianLocation*, const std::list<RadioSignal<RadioDevice>>*> radioMap;
std::vector<RadioDevice> radioDevices;
Run Code Online (Sandbox Code Playgroud)

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

在这一行,radioMap[location] = observedSignals;我结束了这个错误

"无效的二进制表达式操作数('const CartesianLocation'和'const CartesianLocation')"在struct _LIBCPP_TYPE_VIS_ONLY上更少:binary_function <_Tp,_Tp,bool> {_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp&__ x,const _Tp&__y)const {return __x <__y;}};

知道我哪里错了吗?

Rak*_*111 8

您没有为 提供比较器std::map,因此它使用std::less. 但std::less没有重载 for CartesianLocation(并且CartesianLocation没有operator<),因此您会收到错误。

您可以添加operator<

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

另一种方法是定义自定义比较器,例如:

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

然后你可以将它传递给std::map

std::map<CartesianLocation, std::list<RadioSignal<RadioDevice>>, comp> radioMap;
Run Code Online (Sandbox Code Playgroud)