错误:传递'const ...''as'this'参数'...'丢弃限定符 [英] error: passing ‘const ...'’ as ‘this’ argument of ‘...’ discards qualifiers
问题描述
stockListType.cpp:58:实例化自此处
/ usr / include / c ++ / 4.2.1 / bits / stl_algo .h:91:error:传递'const stockType'作为'bool stockType :: operator<(const stockType&)'的'this'参数丢弃限定符
pre>
/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'参数丢弃限定符
上面是我得到的错误,并希望有人解释我的意思。我通过在过载操作符前面放置一个常量来解决错误。我的程序是一个股票市场应用程序,读取一个文件,包括一个字符串,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 inoperator<
function. You should addconst
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 becausestd::sort
usesoperator<
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屋!