non-member function cannot have cv-qualifier

C++中的CV限定符const和volatile不能用于非成员函数和静态成员函数的声明。非成员函数没有this指针,所以不需要cv限定;静态成员函数与类的实例无关,因此也不能使用。示例代码展示了尝试在这些函数上使用cv限定符导致的编译错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

编译产生错误: 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

cc1plus: warning: ‘-Wabi’ won’t warn about anything [-Wabi] cc1plus: note: ‘-Wabi’ warns about differences from the most up-to-date ABI, which is also used by default cc1plus: note: use e.g. ‘-Wabi=11’ to warn about changes from GCC 7 In file included from /usr/include/boost/detail/endian.hpp:9, from /home/csh/pcl-1.8.1/common/include/pcl/PCLPointCloud2.h:11, from /home/csh/pcl-1.8.1/common/include/pcl/pcl_base.h:55, from /home/csh/pcl-1.8.1/segmentation/include/pcl/segmentation/organized_multi_plane_segmentation.h:44, from /home/csh/pcl-1.8.1/segmentation/src/organized_multi_plane_segmentation.cpp:42: /usr/include/boost/predef/detail/endian_compat.h:11:161: note: #pragma message: The use of BOOST_*_ENDIAN and BOOST_BYTE_ORDER is deprecated. Please include <boost/predef/other/endian.h> and use BOOST_ENDIAN_*_BYTE instead 11 | t/predef/other/endian.h> and use BOOST_ENDIAN_*_BYTE instead") | ^ In file included from /home/csh/pcl-1.8.1/segmentation/include/pcl/segmentation/organized_multi_plane_segmentation.h:48, from /home/csh/pcl-1.8.1/segmentation/src/organized_multi_plane_segmentation.cpp:42: /home/csh/pcl-1.8.1/segmentation/include/pcl/segmentation/plane_coefficient_comparator.h: In member functionconst std::vector<float>& pcl::PlaneCoefficientComparator<PointT, PointNT>::getPlaneCoeffD() const’: /home/csh/pcl-1.8.1/segmentation/include/pcl/segmentation/plane_coefficient_comparator.h:145:64: error: non-member functionconst boost::shared_ptr<std::vector<float> >& pcl::getPlaneCoeffD()cannot have cv-qualifier 145 | const boost::shared_ptr<std::vector<float>>& getPlaneCoeffD() const ; | ^~~~~ /home/csh/pcl-1.8.1/segmentation/include/pcl/segmentation/plane_coefficient_comparator.h:147:13: error: invalid initialization of reference of type ‘const std::vector<float>&’ from expression of type ‘const boost::shared_ptr<std::vector<float> >’ 147 | return plane_coeff_d_; | ^~~~~~~~~~~~~~ make[2]: *** [segmentation/CMakeFiles/pcl_segmentation.dir/build.make:167:segmentation/CMakeFiles/pcl_segmentation.dir/src/organized_multi_plane_segmentation.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:1905:segmentation/CMakeFiles/pcl_segmentation.dir/all] 错误 2 make: *** [Makefile:163:all] 错误 2
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值