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 ...
随机推荐
- Python数据类型-7
什么数据类型. int 1,2,3用于计算. bool:True,False,用户判断. str:存储少量数据,进行操作 'fjdsal' '二哥','`13243','fdshklj' '战三,李四 ...
- 个人博客作业_week14
M1/M2阶段总结 我在M1阶段负责后端代码的开发,以及协助PM,在M2阶段负责PM,在为期将近一学期的团队软件开发过程中,我深刻体会到了团队协作的重要性,以及合理分配任务的重要性,没有一个好的时间规 ...
- 四则运算法则在Java中的实现
软件工程的课程已经上过有一段时间了,前段时间由于比较忙着考试,所以关于四则运算的代码一直没有实现.同时由于近来一段时间一直在自学java,因为C++虽然也是面向对象,而且可以开发很多软件或者程序,但是 ...
- 去掉ambiguous expansion of macro警告
查看原文:http://www.heyuan110.com/?p=1221 用pod install后,pod工程里出现ambiguous expansion of macro的warning,对于有 ...
- PHP文件下载功能实现
客户端的浏览器通过HTTP协议可以实现文件下载: 方法一: 能提供用户下载的最简单的方法就是使用一个<a></a>标签,比如在页面中添加这么一行代码 <a href=&q ...
- Undertow的InMemorySessionManager
https://github.com/undertow-io/undertow/blob/master/core/src/main/java/io/undertow/server/session/In ...
- Linux 多主机SSH互信操作---noob....
创建虚拟机后-- ssh-keygen 生成公钥和私钥对 -rw------- 1 root root 1679 11月 4 09:18 id_rsa-rw-r--r-- 1 root root 39 ...
- Jquery 组 标签页
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- python箱型图
#-*- coding: utf-8 -*- import pandas as pd catering_sale = '../data/catering_sale.xls' #餐饮数据 data = ...
- GROUP BY 聚合函数(max min sum avg count) 和HAVING的 使用
重新复习一下这个都源自于我经常需要查的一个需求 “要找到表里面 出现了两次或者三次的某个字段” 现在假设有一张表字段是 +----+---------------------+------------ ...