错误:传递'const ...''as'this'参数'...'丢弃限定符 [英] error: passing ‘const ...'’ as ‘this’ argument of ‘...’ discards qualifiers

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

问题描述

stockListType.cpp:58:实例化自此处

  / usr / include / c ++ / 4.2.1 / bits / stl_algo .h:91:error:传递'const stockType'作为'bool stockType :: operator<(const stockType&)'的'this'参数丢弃限定符
/usr/include/c++/4.2.1/bits/ stl_algo.h:92:error:传递'const stockType'作为'bool的参数'bool stockType :: operator<(const stockType&)'丢弃限定符
/usr/include/c++/4.2.1/bits /stl_algo.h:94:error:传递'const stockType'作为'bool的参数'bool stockType :: operator<(const stockType&)'丢弃限定符
/usr/include/c++/4.2.1/ bits / stl_algo.h:98:error:传递'const stockType'作为'bool stockType :: operator<(const stockType&)'的'this'参数丢弃限定符
/usr/include/c++/4.2.1 /bits/stl_algo.h:100:error:将'const stockType'传递为'bool stockType :: operator<(const stockType&)'的'this'参数丢弃限定符
pre>

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

  //函数在stockType.h中声明并在stockType.cpp中实现
bool operator<(const stockType&股票)//符号是一个字符串
{
return(symbols< stock.symbols)
}


//在stockListType.h中,并在
// stockListType.cpp中实现,其中将stockType的对象实例化为向量。
// vector< stockType>列表;在stockListType.h文件中定义

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


解决方案

错误讯息您正在从运算符< 函数中的对象中转换 const 。您应该将 const 添加到不修改成员的所有成员函数。

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

编译器抱怨 operator< 的原因是因为 std :: sort 使用<$ c $ $ <$> $ <$> c> function。



更新:

  stockType项); 

到:

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


stockListType.cpp:58: instantiated from here

/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.

//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 functions 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());
     }

解决方案

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

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

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

Also you have another syntax error in insert function.

Update:

void insert(const& stockType item);

to:

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

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

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