non-member function cannot have cv-qualifier
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>?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限定。
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;
}
编译产生错误:

意思是:
非成员函数不能有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;
}
编译出现的错误:

意思是:
静态成员函数,不能有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;
}
non-member function cannot have cv-qualifier的更多相关文章
- Thinkphp---------Call to a member function free_result() on a non-object
1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...
- :( Call to a member function Table() on a non-object 错误位置
:( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form
- Fatal error: Call to a member function bind_param() on a non-object in
今天在练习 mysql是出现错误: Fatal error: Call to a member function bind_param() on a non-object in 解决步骤: 1. ...
- ECmall错误:Call to a member function get_users_count() on a non-object
问题描述: 在后台添加了一个app报错:Call to a member function get_users_count()Fatal error: Call to a member functio ...
- magento后台 Fatal error: Call to a member function getId() on a non-object in错误
后台分类管理出现错误 Fatal error: Call to a member function getId() on a non-object in 在数据库中运行以下sql语句 INSERT I ...
- Function语义学之member function
之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...
- Timer.4 - Using a member function as a handler
In this tutorial we will see how to use a class member function as a callback handler. The program s ...
- C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化
模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...
- About The Order of The Declarations And Definition When Making a Member Function a Friend.关于使类成员成为另一个类友元函数的声明顺序和定义。
If only member function clear of WindowMgr is a friend of Screen, there are some points need to note ...
- About Why Inline Member Function Should Defined in The Header File
About why inline member function should defined in the header file. It is legal to specify inline on ...
随机推荐
- 个人作业Week2-代码复审(修改明确了要求)
代码复审 零,说在前面的话 大家完成了个人项目之后,都写了很多代码. 这些代码可能: 大括号换行/不换行 使用tab缩进/使用空格缩进 变量名函数名的定义很好/不好 每个函数都有详细的注释解释函数的功 ...
- java_web—JSP+Servlet+JavaBean
JSP -> Java Server Page 后端 jsp -> JavaScript 前端 JSP语法 1.JSP插入Java代码 三种形式: (1)<%! %> ( ...
- git学习心得
https://github.com/zhangxinn/test/tree/master 自己虽然在课堂上有认真的听老师讲解如何使用github,包括怎样在线学习,怎样在github上建立自己的仓库 ...
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
TitleLayout 多功能.通用的.可在布局或者使用Java代码实现标题栏:支持沉浸式状态栏,支持左侧返回按钮(不需要手动实现页面返回),左侧支持图片+文字.图片.文字:右侧支持图片.文字等. 堆 ...
- PHP预防跨站脚本(XSS)攻击且不影响html代码显示效果
什么是XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往 ...
- 面象对象设计原则之五:依赖倒置原则(The Dependency Inversion Principle,DIP)
如果说开闭原则是面向对象设计的目标的话,那么依赖倒转原则就是面向对象设计的主要实现机制之一,它是系统抽象化的具体实现.依赖倒转原则是Robert C. Martin在1996年为“C++Reporte ...
- [区块链]POW 与POS
POW:全称Proof ofWork,工作证明. 这是什么意思呢?就是说,你能获得多少货币,取决于你挖矿贡献的有效工作,也就是说,你电脑性能越好,分给你的矿就会越多,这就是根据你的工作证明来执行货币的 ...
- Docker 网络部分的简单学习以及转帖别人的blog
1. 感谢一下 大神: http://www.cnblogs.com/sparkdev/ 最近有时间的话 就会读一下他的博客.学习了解docker相关的知识 今天简单做了下 测试 在这里面记录一下. ...
- [日常工作]GS使用安装盘修改密码后的处理
1. GS服务器端有时候需要修改 9999 的用户密码.但是修改完密码之后有几个注意事项: 一般有两个可以修改注册的地方: 使用setup里面的修改用户密码 修改完密码之后要手工注册一下数据库实例 也 ...
- 关于Spring MVC跨域
1.Sping MVC 3.X跨域 关于跨域问题,主要用的比较多的是cros跨域. 详细介绍请看https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Ac ...