zoukankan      html  css  js  c++  java
  • 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>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;
    }

    编译产生错误:

    C++编程常见错误—cannot have cv-qualifier//不能有CV限定,在C++中CV指const和volatile - 无影 - 激情、专注、坚持、思考

     意思是:

    非成员函数不能有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;
    }

    编译出现的错误:

    C++编程常见错误—cannot have cv-qualifier//不能有CV限定,在C++中CV指const和volatile - 无影 - 激情、专注、坚持、思考

     意思是:

    静态成员函数,不能有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;
      }

  • 相关阅读:
    ubuntu 如何 su 到 root(作为 root 用户操作)
    centos6.5 redis 安装配置及java调用
    springmvc 国际化
    springmvc 整合数据验证框架 jsr
    springmvc 整合shiro
    centos 6.5 安装mysql
    hive 报错 java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient
    centos 关闭防火墙
    client.HConnectionManager$HConnectionImplementation: Can't get connection to ZooKeeper: KeeperErrorCode = ConnectionLoss for /hbase
    fms +fme 视频直播
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/9237308.html
Copyright © 2011-2022 走看看