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. 《剑指offer》面试题27 二叉搜索树与双向链表 Java版

    (将BST改成排序的双向链表.) 我的方法一:根据BST的性质,如果我们中序遍历BST,将会得到一个从小到大排序的序列.如果我们将包含这些数字的节点连接起来,就形成了一个链表,形成双向链表也很简单.关 ...

  2. [BZOJ 3771] Triple(FFT+容斥原理+生成函数)

    [BZOJ 3771] Triple(FFT+生成函数) 题面 给出 n个物品,价值为别为\(w_i\)且各不相同,现在可以取1个.2个或3个,问每种价值和有几种情况? 分析 这种计数问题容易想到生成 ...

  3. 原生js事件委托(事件代理)方法扩展

    原生js事件委托(事件代理)方法扩展: 通过Node底层原型扩展委托方法 /** * 事件委托方法 * @param eventName {string}:事件名称,如'click' * @param ...

  4. [VINS]IMU与相机之间旋转量的标定

    VINS-Mono[1]中IMU-Camera外参旋转量\(R_b^c\)的计算方法在他们实验室发的之前的论文有详细讲解[2].视觉利用匹配特征点中的基础矩阵求出相机坐标系下两帧的旋转量\(R_{c_ ...

  5. 深度学习之depthwise separable convolution,计算量及参数量

    目录: 1.什么是depthwise separable convolution? 2.分析计算量.flops 3.参数量 4.与传统卷积比较 5.reference

  6. 关于jsp删除成功,添加成功等之后 页面自动跳转的js写法

    因为比较常用,所以写在博客里保存起来,防止以后忘了不会写了: 删除成功,<span id="time" style="background:red"> ...

  7. [NOI2018]归程(kruscal重构树)

    [NOI2018]归程 题面太长辣,戳这里 模拟赛上写了一个spfa (关于spfa,它已经死了),然后一个st表水完暴力跑路.考后说是Kruscal重构树或者可持久化并查集???这都是些什么东西.不 ...

  8. Azure IoT 技术研究系列1

    物联网技术已经火了很多年了,业界各大厂商都有各自成熟的解决方案.我们公司主要搞新能源汽车充电,充电桩就是我们物联网技术的最大应用,车联网.物联网. 互联网三网合一.作为Azure重要的Partner和 ...

  9. hashcode 和 equals

    https://www.cnblogs.com/Qian123/p/5703507.html#_label0 hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值 详细了解 ...

  10. 35.ES6语法介绍——2019年12月24日

    2019年12月24日16:22:24 2019年10月09日12:04:44 1. ES6介绍 1.1 新的 Javascript 语法标准 --2015年6月正式发布 --使用babel语法转换器 ...