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. tensorflow学习笔记七----------RNN

    和神经网络不同的是,RNN中的数据批次之间是有相互联系的.输入的数据需要是要求序列化的. 1.将数据处理成序列化: 2.将一号数据传入到隐藏层进行处理,在传入到RNN中进行处理,RNN产生两个结果,一 ...

  2. tensorflow学习笔记三----------基本操作

    tensorflow中的一些操作和numpy中的很像,下面列出几个比较常见的操作 import tensorflow as tf #定义三行四列的零矩阵 tf.zeros([3,4]) #定义两行三列 ...

  3. HNUSTOJ-1695 跳格子(略感头疼)

    1695: 跳格子 时间限制: 1 Sec  内存限制: 128 MB提交: 230  解决: 57[提交][状态][讨论版] 题目描述 逸夫楼的大厅的地面有10行10列的石砖,我们用坐标(x,y)来 ...

  4. struts2_对Map进行双层迭代

    转自:struts2_对Map进行双层迭代 //后台数据 public String execute() throws Exception { Map<String, List<Produ ...

  5. Python 通过dmidecode获取Linux服务器硬件信息

    通过 dmidecode 命令可以获取到 Linux 系统的包括 BIOS. CPU.内存等系统的硬件信息,这里使用 python 代码来通过调用 dmidecode 命令来获取 Linux 必要的系 ...

  6. IDEA创建maven的web项目时,main文件夹下没有java,resources目录等源文件夹

    https://blog.csdn.net/qq_34377273/article/details/83183307

  7. win10系统中photoshop cs6中界面字体太小的解决方法

    win10系统中photoshop cs6中界面字体太小的解决方法 ps页面系统字体特别小,解决方法如下: 右键ps图标—属性—兼容性—勾选替代高dpi缩放行为,缩放执行选为默认,如果不行三个都试试

  8. 非父子组件通过事件传值-vue

    1.创建中央事件总线:额外的 new Vue()2.$emit 触发事件3.$on 监听事件 在使用组件的文件中: <template> <div> <x-test :b ...

  9. Thymeleaf 模板引擎简介

    目录 Thymeleaf 模板引擎 官方文档下载 Hello World 新建应用 后台控制器 前端页面 浏览器访问测试 Thymeleaf 模板引擎1.Thymeleaf 是 Web 和独立环境的现 ...

  10. Arduino库

    单总线库: 下载地址  :  链接:https://pan.baidu.com/s/1YSuqrXWuBAxMEUWHy8rckw    提取码:svix 把整个文件夹复制到 Arduino安装目录的 ...