﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-cppblogs</title><link>http://www.cppblog.com/cppblogs/</link><description /><language>zh-cn</language><lastBuildDate>Wed, 13 May 2026 19:26:50 GMT</lastBuildDate><pubDate>Wed, 13 May 2026 19:26:50 GMT</pubDate><ttl>60</ttl><item><title>关于c++ error ： passing " "as" " discards qualifiers</title><link>http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html</link><dc:creator>Gage</dc:creator><author>Gage</author><pubDate>Thu, 06 Sep 2012 14:19:00 GMT</pubDate><guid>http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html</guid><wfw:comment>http://www.cppblog.com/cppblogs/comments/189749.html</wfw:comment><comments>http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cppblogs/comments/commentRss/189749.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cppblogs/services/trackbacks/189749.html</trackback:ping><description><![CDATA[<div>今天写了一段小代码，本以为正确，但运行后，就somehow &#8221;discard qualifier&#8220;<br />于是猛百度，google之，找到答案，可惜正解为英语，冗长之，自己翻译半天，终于弄明白~<br />这里是原程序：<br />#include&lt;iostream&gt;</div><div>using namespace std;</div><div>class Date</div><div>{</div><div>&nbsp; &nbsp; &nbsp; int year;</div><div>&nbsp; &nbsp; &nbsp; public:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Date(int y):year(y){}</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int get_year()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return year;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int &nbsp;plus(const Date&amp; p)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int total = p.get_year()+year;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return total;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>};&nbsp;</div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; Date q(1000);</div><div>&nbsp; &nbsp; Date p(2000);</div><div>&nbsp; &nbsp; cout&lt;&lt;p.plus(q);</div><div>&nbsp; &nbsp; system("pause");</div><div>}</div><div>当你一运行必然，passing `const Date' as `this' argument of `int Date::get_year()' discards qualifiers 这行字是什么意思呢？原来const Date&amp;p,编译器认定调用 const member function,也就是不能把p调用的成员值修改，自习看get_year,只是read，并没有modify啊？按理说没有modify，但是c++编译器总是假定你可以将值修改，事实也是如此，的确可将值修改，所以与const member function不符，怎么改两种方法。第一，去掉const。第二，在get_year 后加const标记<br /><div>#include&lt;iostream&gt;</div><div>using namespace std;</div><div>class Date</div><div>{</div><div>&nbsp; &nbsp; &nbsp; int year;</div><div>&nbsp; &nbsp; &nbsp; public:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Date(int y):year(y){}</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int get_year()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return year;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int &nbsp;plus(const Date&amp; p)&nbsp;<span style="color: red; ">const</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int total = p.get_year()+year;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return total;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>};&nbsp;</div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; Date q(1000);</div><div>&nbsp; &nbsp; Date p(2000);</div><div>&nbsp; &nbsp; cout&lt;&lt;p.plus(q);</div><div>&nbsp; &nbsp; system("pause");</div><div>}</div><div>这样就对了。<br />以上仅是初学者一点思考，供大家一哂~~~<br /></div></div><img src ="http://www.cppblog.com/cppblogs/aggbug/189749.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cppblogs/" target="_blank">Gage</a> 2012-09-06 22:19 <a href="http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>