错误:传递const xxx作为xxx的此参数抛弃限定符 [英] Error: passing const xxx as this argument of xxx discards qualifiers

查看:138
本文介绍了错误:传递const xxx作为xxx的此参数抛弃限定符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题将我的函子从windows移植到linux。 (函数传递给stl :: map严格弱序)
原来如下:

I'm having an issue porting my functor from windows to linux. (a functor to pass to stl::map for strict-weak ordering) The original is as follows:

struct stringCompare{ // Utilized as a functor for stl::map parameter for strings 
    bool operator() (string lhs, string rhs){ // Returns true if lhs < rhs
        if(_stricmp(lhs.c_str(), rhs.c_str())  < 0) return true;
        else return false;
    }
};

由于linux不支持_stricmp,而是使用strcasecmp,我将其更改为:

As linux doesnt support _stricmp but uses strcasecmp instead, I changed it to:

struct stringCompare{ 
    bool operator() (string lhs, string rhs){ // Returns true if lhs < rhs
        if(strcasecmp(lhs.c_str(), rhs.c_str())  < 0) return true;
        else return false;
    }
};

现在抱怨const参数:

And it is now complaining about "const" parameters:

passing const stringCompare as this argument of bool stringCompare::operator()  
(std::string, std::string)â discards qualifiers

我不完全确定为什么它假设stringCompare应该是一个常数...

I'm not entirely sure why it supposes stringCompare should be a constant...

对于这个被实例化的线是:

And the line where it is mad about this being instantiated is:

if(masterList->artistMap.count(songArtist) == 0) 

artistMap是一个带有字符串键的stl :: map 。

artistMap being an stl::map with a string key.

我不知道我会出错。我试图将bool operator()参数更改为const,因为它似乎抱怨某种类型的非常量参数传递。这不工作,也没有改变'bool operator()'到'const bool operator()'。

I'm not sure where I'm going wrong. I attempted to change the bool operator() parameters to const, as it appears that it's complaining about some sort of non-constant parameter passing. This didn't work, nor did changing 'bool operator()' to 'const bool operator()'.

据我所知,strcasecmp是一个const函数所以,如果我传递它是非常量或常量参数(c_str()也是const),所以我不知道我会错误。

As far as I know, strcasecmp is a const function so should case whether I pass it non-constant or constant parameters (c_str() also being const), so I'm not exactly certain where I'm going wrong.

我已经google了类似的问题,但我仍然不能理解这个问题,从我在stackoverflow和几个其他地方看到的。

I've googled similar issues but I still can't quite make sense of the issue from what I've seen both on stackoverflow and a couple other places.

数据类型,其中我使用的是:

The datatype where I'm using this is:

map<string, set<song, setSongCompare>*,stringCompare > artistMap;


推荐答案

两件事:


  1. 定义 bool operator() const 。这只是一个好的做法。这告诉编译器这个函数不会对类的成员变量产生副作用。

  1. Define your bool operator() as const. It's just a good practice. This tells the compiler that this function will not have side-effects on the member variables of the class.

添加 const& / code>参数中的 / code>限定符。传递常量引用而不是复制内存在所有地方也是好办法。通过声明引用 const ,你告诉编译器这个函数不应该对被引用的对象有副作用。

Add const & qualifiers to the lhs and rhs arguments. Passing constant references instead of copying memory all over the place is also good practice. By declaring references as const you're telling compiler that this function should not have side-effects on the referenced objects.

您的运算符()应如下所示:

bool operator() (const string &lhs, const string &rhs) const 
{
  return strcasecmp(lhs.c_str(), rhs.c_str())  < 0;
}

这篇关于错误:传递const xxx作为xxx的此参数抛弃限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆