http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html

今天写了一段小代码,本以为正确,但运行后,就somehow ”discard qualifier“
于是猛百度,google之,找到答案,可惜正解为英语,冗长之,自己翻译半天,终于弄明白~
这里是原程序:
#include<iostream>
using namespace std;
class Date
{
      int year;
      public:
        Date(int y):year(y){}
        int get_year()
        {
          return year;
        }
      
        int  plus(const Date& p)
        {
           int total = p.get_year()+year;
           return total;
        }
}; 
int main()
{
    Date q(1000);
    Date p(2000);
    cout<<p.plus(q);
    system("pause");
}
当你一运行必然,passing `const Date' as `this' argument of `int Date::get_year()' discards qualifiers 这行字是什么意思呢?原来const Date&p,编译器认定调用 const member function,也就是不能把p调用的成员值修改,自习看get_year,只是read,并没有modify啊?按理说没有modify,但是c++编译器总是假定你可以将值修改,事实也是如此,的确可将值修改,所以与const member function不符,怎么改两种方法。第一,去掉const。第二,在get_year 后加const标记

#include<iostream>
using namespace std;
class Date
{
      int year;
      public:
        Date(int y):year(y){}
        int get_year()
        {
          return year;
        }
      
        int  plus(const Date& p) const
        {
           int total = p.get_year()+year;
           return total;
        }
}; 
int main()
{
    Date q(1000);
    Date p(2000);
    cout<<p.plus(q);
    system("pause");
}
这样就对了。
以上仅是初学者一点思考,供大家一哂~~~

关于c++ error : passing " "as" " discards qualifiers的更多相关文章

  1. C++ error: passing 'const std::map<>]' discards qualifiers或pass-by-reference-to-const-map导致的“discards qualifiers”

    产生问题的场景: int func(const map<int, string> &aMap) { string value = amap[0]; } 或者 int  Test:: ...

  2. passing ‘const ’ as ‘this’ argument of ‘’ discards qualifiers 错误处理

    示例程序: #include <iostream> #include <set> using   namespace std ; class   StudentT { publ ...

  3. invalid initialization of non-const reference of type与discards qualifiers

    参数传递          函数参数的传递是初始化语义:用调用者的实参去初始化函数的形参,如果参数是对象,需要调用该类的拷贝构造函数,如果没有显式定义的拷贝构造函数,则执行默认的按成员拷贝      ...

  4. 实参时丢弃了类型 discards qualifiers discards qualifiers问题

    百思不得其解,于是百度,google吧.. 发现Stackoverflow上也有人有相同的问题 下面是他的问题: For my compsci class, I am implementing a S ...

  5. C++ 编译报错discards qualifiers [-fpermissive]

    声明了一个类 class Card { public: Card(const string&); int m_value; char m_suit; private: const static ...

  6. c++ 异常 discards qualifiers 丢弃

    src/feedbackservice.cpp:76: error: passing `const ps::spider::urlreceiver::entry::ConfigManager' as ...

  7. passing argument 3 of ‘wtk_hlv_rec_init’ discards ‘const’ qualifier from pointer target type

    -Werror,编译出现如下错误: src/wtk/exam/wtk_ndx.c:154:6: error: passing argument 3 of ‘wtk_hlv_rec_init’ disc ...

  8. QT210 android2.3 和android4.0 烧写编译日记

    QT210下载烧录编译android2.3过程 工作环境:ubuntu12.04.5 | QT210开发板光盘 | QT210开发板 android2.3编译环境:gcc version 4.4.7  ...

  9. 「c++小学期」实验题目及代码

    面向对象编程的C++,和平时做题用的C++还是有差距的.实验的题目都是小题目,就都做一下吧.(没放代码的为要验收的 实验一 简单C++程序设计 1.  猜价格游戏 编写C++程序完成以下功能: (1) ...

随机推荐

  1. shell脚本又是什么?

    操作系统命令的组合称为Shell脚本: 一个Shell脚本主要由原本需要在命令行输入的命令组成,或在一个文本编辑器中,用户可以使用脚本来把一些常用的操作组合成一组串行.主要用来书写这种脚本的语言叫做脚 ...

  2. 一分钟搭建 Web 版的 PHP 代码调试利器

    一.背景   俗话说:"工欲善其事,必先利其器".作为一门程序员,我们在工作中,经常需要调试某一片段的代码,但是又不想打开繁重的 IDE (代码编辑器).使用在线工具调试代码有时有 ...

  3. TScreen研究(有待研究)

    先扔在这里,待研究: http://blog.csdn.net/lailai186/article/details/8141170 procedure TForm1.Button1Click(Send ...

  4. kafka复习(2)

    一.简介 --------------------------------------- 1.kafka是一个分布式的.可分区的.可复制的消息系统.它提供了消息系统的功能,但是有自己独特的设计. 2. ...

  5. div和span互换

    div是块级元素,它不论大小默认占一行,而且可以设置宽高以及外边距span是行内元素,它占它自身大小的位置,而且不能设置宽高以及边距同时div也可以变为span (display:inline),这样 ...

  6. 如何编写testbench的总结(非常实用的总结)

    1.激励的设置 相应于被测试模块的输入激励设置为reg型,输出相应设置为wire类型,双向端口inout在测试中需要进行处理. 方法1:为双向端口设置中间变量inout_reg作为该inout的输出寄 ...

  7. Docker 内pip安装package报错: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'

    说来奇幻(对本菜来说, 经常遇到堪称奇幻的问题) 之前在docker里面各种安装都没问题, 也不知道什么引起的, 昨天晚上调试的时候卸载了一个包的版本,然后就安不上了. 宿主机安装依然各种流畅,唯独d ...

  8. C#基础知识之依赖注入

    目录 1 IGame游戏公司的故事 1.1 讨论会 1.2 实习生小李的实现方法 1.3 架构师的建议 1.4 小李的小结 2 探究依赖注入 2.1 故事的启迪 2.2 正式定义依赖注入 3 依赖注入 ...

  9. PHP file函数

    一.判断函数 is_file($filename) //判断是否文件 is_link($filename) //判断是否为链接符号 is_dir($filename) //判断是否为路径 is_rea ...

  10. <转>ThinkPHP的开发常用系统配置项

    /* 项目设定 */ ’APP_DEBUG’ => false, // 是否开启调试模式 ’APP_DOMAIN_DEPLOY’ => false, // 是否使用独立域名部署项目 ’AP ...