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限定。

       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;
}

编译产生错误:

意思是:

非成员函数不能有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的更多相关文章

  1. Thinkphp---------Call to a member function free_result() on a non-object

    1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...

  2. :( Call to a member function Table() on a non-object 错误位置

    :( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. Function语义学之member function

    之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...

  7. 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 ...

  8. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Linux 第七周实验 及总结

    姬梦馨 原创作品 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 第七周 Linux内核如何装载和启动一 ...

  2. QT QProgressBar QProgressDialog 模态,位置设置,无边框,进度条样式

    一  关于模态设置 QProgressDialog可以设置模态(需要在new的时候传入parent),QProgressBar设置不好: 只有dialog可以设置模态,widget不能设置模态(QPr ...

  3. 第一个Sprint第一天

    第一个Sprint冲刺的第一天 组员:陈建定 陈友沛 林清松 我们小组选的题目是小学四则运算APP 之前我们都没有做过这个程序,不过我们会尽力完成. 第一阶段的冲刺我们主要分析这个APP的所需的功能, ...

  4. JavaScript使用childNodes和children

    childNodes用来获取一个元素的所有子元素,这个包括元素节点和文本节点. children用来获取一个元素的子元素节点,注意只是元素节点 其中DOM中常见的三种节点分别如下: 元素节点:< ...

  5. [转帖] BIO与NIO、AIO的区别

    培训里面讲的东西  自己查了下 啥意思,,, 转帖强化一下. http://blog.csdn.net/skiof007/article/details/52873421 IO的方式通常分为几种,同步 ...

  6. zlib 简单封装

    下列代码用于压缩和解压字符串,使用标准库string.实现了对zlib的简单封装. #pragma once #include <boost/noncopyable.hpp> #inclu ...

  7. CSS 选择器继承和层叠

    CSS选择器及其继承特性.层叠特性1.基本选择器  标记  id  class  这个就不再作介绍了 2.复合选择器  交集 交集选择器由两个选择器直接连接构成,其结果是选中二者各自元素范围的交集 其 ...

  8. 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看)

    前两天朋友发给我了一篇文章,是携程网反爬虫组的技术经理写的,大概讲的是如何用他的超高智商通过(挑衅.怜悯.嘲讽.猥琐)的方式来完美碾压爬虫开发者.今天我就先带大家开发一个最简单低端的爬虫,突破携程网超 ...

  9. Log4J日志信息配置文件详解

    原文地址: http://blog.csdn.net/wuxintdrh/article/details/78282097 使用log4j 记录日志甚是方便,其提供了两种日志配置方式,log4j.pr ...

  10. SpringBoot基础入门

    1.SpringBoot核心相关内容 1.1入口类 SpringBoot通常有一个入口类*Application,内部有一个main方法,是启动SpringBoot的入口.使用@SpringBootA ...