non-member function cannot have cv-qualifier

阅读目录

  • 在C++中CV指const和volatile—
  • 1、非成员函数不能有CV限定,友元函数不是类的成员函数声明友元函数不能用const限定。
  • 2、静态成员函数不能有CV限定
Q: non-member function unsigned int abs(const T&) cannot have cv-qualifier.template<typename T>
inline unsigned int abs(const T& t) const
{return t>0?t:-t;
}ans: The ending const specifies that you will not modify any member variables of a class belongs to.
Free functions (and class static functions) don't have a this pointer.

在C++中CV指const和volatile—

1、非成员函数不能有CV限定,友元函数不是类的成员函数声明友元函数不能用const限定。

friend voin fun(classname &ref)const;//编译器会给错误error: non-member function ‘xxxxxxxxx’ cannot have cv-qualifier

2、静态成员函数不能有CV限定

情况一、在C++中,非成员函数不能含有CV限定,即const和volatile限定

#include <iostream>
using namespace std;

double getArea() const
{
    return 0.0;
}
double getVolume() const
{
    return 0.0;
}

int main(int arg,char *argv[])
{
    cout << getArea() << endl;
    cout << getVolume() << endl;
    return 0;
}

编译产生错误: non-member function cannot have cv-qualifier

意思是:

非成员函数不能有cv 限定符,cv 限定符 c++有两个:const 和 volatile,这儿指const  。

情况二、在C++中,静态成员函数不能有CV限定,即const和volatile限定

头文件static_cpp.h

#ifndef __STATIC_H
#define __STATIC_H

class CStatic 
{
    private:
        static int static_value;
    public: 
        static int get_static_value() const;          //当不是static方法时,可以用const进行限定。
};

#endif

源文件staitc_cpp.cpp

#include "static_cpp.h"

int CStatic::get_static_value() const 
{
        return static_value;
}

在main.cpp中

#include "static_cpp.h"
#include <iostream>
using namespace std;

int CStatic::static_value = 1;

int main(int argc,char *argv[])
{
    cout << CStatic::get_static_value()<<endl;
    return 0;
}

编译出现的错误:non-member function cannot have cv-qualifier

意思是:

静态成员函数,不能有CV限定符,在C++中CV限定符指const和volatile,这儿指const。

情况三:

#include<iostream>

#include<string>

using namespace std;
  
   class Animal{
     public:
     
    friend void showRes(Animal &ref)const;//这行编译器会给错误non-member function ‘void showRes(Animal&)’ cannot have cv-qualifier 
   friend void showRes(Animal &ref)const;
    private:
     int age;
     string name;
  };

void showRes(Animal &ref){
    ref.age=15;
    ref.name="panda";
    cout << ref.age << ref.name << endl;
  }

转载:https://www.cnblogs.com/guxuanqing/p/9237308.html

non-member function cannot have cv-qualifier相关推荐

  1. [C++]Call virtual member function in constructor or destructor

    嗷嗷按,今天被问到在constructor/destructor中调用virtual member function的问题.答错了,很羞耻. 依稀记得在constructor/destructor调用 ...

  2. error: reference to non-static member function must be called sort(nums.begin(),nums.end(),cmp1)

    leetcode179题中编译出现则个问题 出错的代码: class Solution { public:bool cmp1(const int& a, const int& b){s ...

  3. call to member function bind_param() on boolean...........

    Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in .... Stack trace: ...

  4. 提示illegal reference to data member'CPMAgentManageDlg::m_matrixMatrixSt'in a static member function

    当提示"illegal reference to data member'CPMAgentManageDlg::m_matrixMatrixSt'in a static member fun ...

  5. 出现错误illegal call of non-static member function?

    当出现错误"illegal call of non-static member function"? 解决方法:原因在于没有经过对象,就调用了非静态成员函数,只有静态成员函数(st ...

  6. c++ 静态类成员函数(static member function) vs 名字空间 (namespace)

    好多人喜欢把工具函数做成static member function.这样以增加隐蔽性和封装性,由其是从C#,java转而使用c++的开发人员. 例如: class my_math { public: ...

  7. NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

    NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. ...

  8. warning C4183: ‘Cricle‘: member function definition looks like a ctor, but name does not match enclo

    报错:warning C4183: 'Cricle': member function definition looks like a ctor, but name does not match en ...

  9. C/C++编程:reference to non-static member function must be called

    主要原因是我使用了函数指针,而函数指针所指函数须得是静态才行 错误代码: class Solution {public:string ReverseSentence(string str) {type ...

  10. invalid use of non-static member function

    现在有一个 Object 类,它有两个方法: Recv 负责接收数据,并传递给 Process Process 负责处理数据 我们希望这个类接收完数据后启动线程去处理,不阻塞后面的其他业务逻辑. cl ...

最新文章

  1. 如何在单页应用程序Angular 7中使用FastReport Core Web报表
  2. BZOJ 1815: [Shoi2006]color 有色图 [Polya DFS 重复合并]
  3. mysql plugin filed_MySQL启动时报Plugin 'InnoDB' registration as a STORAGE ENGINE failed.错误
  4. python中的__call__和__repr__魔术方法
  5. 源码注释性容器的创建及初始化
  6. 返回顶部小火箭(仿电脑管家)
  7. 一文理清Cookie、Session、Token
  8. spring mvc学习(43):处理静态资源
  9. 集合类接口和类层次关系图
  10. 10件产品3件次品,7件正品,不放回的取,第三次取得次品的概率-----三种解法
  11. python基础教程之pymongo库
  12. 《游戏设计信条》【笔记】
  13. 分支定界法求解整数规划
  14. win32项目中使用 skia渲染的一个编译问题
  15. 使用FileZilla,FTP登录出现错误:FileZilla状态: 不安全的服务器,不支持 FTP over TLS
  16. 元核云赋能银行业,智能双录产品助力银保业务合规高效响应
  17. 后台管理制作首页组件及ui框架的使用
  18. C++:二维数组参数传递
  19. 入门【必学】20个SEO优化术语
  20. 801a qcn文件IMEI修改

热门文章

  1. WR:Tetrasphaera PAO 代谢中的储能物质与微生物多样性及除磷效能之间的关系
  2. 扩增子和宏基因组数据分析流程和可视化方案—刘永鑫(南京,2020年11月27日)
  3. Nat. Ecol. Evol.:不想当化学家的物理学家不是好的生物学家
  4. R语言使用ggplot2包使用geom_boxplot函数绘制基础分组箱图(配置数据点抖动显示jitter)实战
  5. sklearn使用投票回归VotingRegressor算法构建多模型融合的投票回归模型、并自定义子回归器的权重(weights)、评估多模型融合的回归模型、评估R2、mse、rmse、mape
  6. R语言ggplot2可视化:为层次聚类树状图dendrogram中的簇进行着色、在树状图dendrogram中为不同的层次聚类簇配置不同的色彩
  7. replace函数对dataframe中的值进行替换(所有数据列中的相同值)
  8. pandas读取字典(dict)数据
  9. UI+UE+UX+区别
  10. 流形学习之LLE(LocallyLinearEmbedding)模型