C++ 错误:将“const ...”作为“...”的“this”参数传递会丢弃限定符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19073996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:29:26  来源:igfitidea点击:

error: passing ‘const ...'’ as ‘this’ argument of ‘...’ discards qualifiers

c++

提问by Emy

stockListType.cpp:58: instantiated from here

stockListType.cpp:58:从这里实例化

/usr/include/c++/4.2.1/bits/stl_algo.h:91: error: passing ‘const stockType' as ‘this' argument of ‘bool stockType::operator<(const stockType&)' discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:92: error: passing ‘const stockType' as ‘this' argument of ‘bool stockType::operator<(const stockType&)' discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:94: error: passing ‘const stockType' as ‘this' argument of ‘bool stockType::operator<(const stockType&)' discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:98: error: passing ‘const stockType' as ‘this' argument of ‘bool stockType::operator<(const stockType&)' discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:100: error: passing ‘const stockType' as ‘this' argument of ‘bool stockType::operator<(const stockType&)' discards qualifiers

Above is the error I got and would like someone to explain to me what it means. I solved the error by placing a constant in front of the overloading operator. My program was a stock market application that read a file that includes a string, 5 doubles and an int. We sort out the program by the string symbols and the index gain. The book instructed me to use vectors to store each data. As you see below the overload operator compares each symbol and sorts it out using the sort member function of containers. My question is why did I have to put a constant in front of the overload operator for > and <. but not for >=, <=, ==, != overload operators.

以上是我得到的错误,希望有人向我解释它的含义。我通过在重载运算符前面放置一个常量来解决这个错误。我的程序是一个股票市场应用程序,它读取包含一个字符串、5 个双精度数和一个整数的文件。我们通过字符串符号和索引增益来整理程序。这本书指示我使用向量来存储每个数据。正如您在下面看到的,重载运算符比较每个符号并使用容器的 sort 成员函数对其进行排序。我的问题是为什么我必须在 > 和 < 的重载运算符前放置一个常量。但不适用于 >=、<=、==、!= 重载运算符。

//function was declared in stockType.h and implemented in stockType.cpp
bool operator<(const stockType& stock)//symbol is a string 
{
  return (symbols < stock.symbols)
}


 //The function below was defined in stockListType.h and implemented in 
 //   stockListType.cpp where I instantiated the object of stockType as a vector.
   //vector<stockType> list; was defined in stockListType.h file

   void insert(const& stockType item)
   {
      list.push_back(item);
      }
  void stockListType::sortStockSymbols()
    {
     sort(list.begin(), list.end());
     }

回答by billz

The error message tells you that you that you are casting of constfrom your object in operator<function. You should add constto all member functions that don't modify member.

错误消息告诉您,您正在constoperator<函数中的对象进行转换。您应该添加const到所有不修改成员的成员函数。

bool operator<(const stockType& stock) const
//                                     ^^^^^
{
  return (symbols < stock.symbols)
}

The reason why compiler complains about operator<is because std::sortuses operator<to compare the elements.

编译器抱怨的原因operator<是因为std::sort用于operator<比较元素。

Also you have another syntax error in insertfunction.

此外,您还有另一个语法错误insert

Update:

更新:

void insert(const& stockType item);

to:

到:

void insert(const stockType& item);
//                         ^^