我正在将我的函数对象从Windows移植到Linux时遇到了问题。(这是一个传递给stl::map用于严格弱排序的函数对象)原始代码如下:
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,因此我将其更改为:
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"参数:
passing const stringCompare as this argument of bool stringCompare::operator()
(std::string, std::string)â discards qualifiers
我不完全确定为什么它认为stringCompare应该是一个常量...
而且,它报错的行是:
if(masterList->artistMap.count(songArtist) == 0)
artistMap是一个带有字符串键的stl::map。
我不确定我的问题在哪里。我尝试将bool operator()参数更改为const,因为它似乎抱怨某种非常数参数传递。这并没有起作用,将'bool operator()'更改为'const bool operator()'也没有起作用。
据我所知,strcasecmp是一个const函数,所以无论我传递非常数或常数参数(c_str()也是const),都应该不会出错,所以我不确定自己错在哪里。
我已经在stackoverflow和其他几个地方搜索了类似的问题,但仍然无法从中理解问题。
我正在使用此数据类型:
map<string, set<song, setSongCompare>*,stringCompare > artistMap;
const
限定符有关吗?警告是在map
声明处还是其他地方报告的? - Kashyapif(masterList->artistMap.count(songArtist) == 0)
其中songArtist是一个字符串。 - Glem