C/C++知识点之C++语言学习(五)——C++语言中的CV限定符错误
小标 2019-04-22 来源 : 阅读 1733 评论 0

摘要:本文主要向大家介绍了C/C++知识点之C++语言学习(五)——C++语言中的CV限定符错误,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之C++语言学习(五)——C++语言中的CV限定符错误,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。


 一、CV限定符错误简介


1、CV限定符简介


CV限定符即cv-qualifier,C++语言中指const和volatile限定符。
通常,C++语言中有两种情况不能使用CV限定符进行限定:
A、非成员函数不能使用CV限定
B、静态成员函数不能使用CV限定


2、CV限定符错误信息简介


C++语言中CV限定符错误信息如“cannot have cv-qualifier”,常见的CV限定符错误信息如下:
A、非成员函数的CV限定符错误信息
error: non-member function 'xxxxxxxxx' cannot have cv-qualifier
B、静态成员函数的CV限定符错误信息
error: static member function 'static xxxxxxx' cannot have cv-qualifier


二、C++语言的CV限定场合


1、非成员函数


在C++中,非成员函数不能含有CV限定,即const和volatile限定。


#include

using namespace std;

double sum(double a, double b)const
{
    return a + b;
}
//error: non-member function 'double sum(double, double)' cannot have cv-qualifier

int main(int argc, char *argv[])
{
    double a = 3.14;
    double b = 2.0;
    double ret = sum(a, b);
    return 0;
}


上述代码报错,非成员函数sum不能使用const进行限定。


#include

using namespace std;

double sum(double a, double b)volatile
{
    return a + b;
}
//error: non-member function 'double sum(double, double)' cannot have cv-qualifier

int main(int argc, char *argv[])
{
    double a = 3.14;
    double b = 2.0;
    double ret = sum(a, b);
    return 0;
}


上述代码报错,非成员函数sum不能使用volatile进行限定。
非成员函数不能使用const和volatile对函数进行限制,但在函数中或函数的返回值可以使用const和volatile进行限定。


#include

using namespace std;

volatile double sum(double a, double b)
{
    volatile double c = a + b;
    return c;
}

int main(int argc, char *argv[])
{
    double a = 3.14;
    double b = 2.0;
    double ret = sum(a, b);
    return 0;
}


类的友元函数不是类的成员函数,不能使用const和volatile对友元函数进行限制。


#include

using namespace std;

class Test
{
private:
    static int data;
public:
    Test()
    {
        data++;
    }
    //静态成员函数
    static int count()
    {
        return data;
    }
    friend int getValue()const;//error
    //error: non-member function 'int getValue()' cannot have cv-qualifier
    ~Test()
    {
        data--;
    }
};

int Test::data = 0;
//error: non-member function 'int getValue()' cannot have cv-qualifier
int getValue()const//error
{
    return Test::data;
}

int main(int argc, char *argv[])
{
    cout << Test::count() << endl;
    Test test;
    cout << test.count() << endl;
    return 0;
}


上述代码报错,类的友元函数getValue不能使用const和volatile进行限定。


2、静态成员函数


在C++中,静态成员函数不能有CV限定,即const和volatile限定。


#include

using namespace std;

class Test
{
private:
    static int data;
public:
    Test()
    {
        data++;
    }
    //静态成员函数
    static int count()const
    {
        return data;
    }
    //error: static member function 'static int Test::count()' cannot have cv-qualifier
    ~Test()
    {
        data--;
    }
};
int Test::data = 0;

int main(int argc, char *argv[])
{
    cout << Test::count() << endl;
    Test test;
    cout << test.count() << endl;
    return 0;
}


上述代码报错,类的静态成员函数count不能使用const关键词进行限定。


#include

using namespace std;

class Test
{
private:
    static int data;
public:
    Test()
    {
        data++;
    }
    //静态成员函数
    static int count()volatile
    {
        return data;
    }
    //error: static member function 'static int Test::count()' cannot have cv-qualifier
    ~Test()
    {
        data--;
    }
};
int Test::data = 0;

int main(int argc, char *argv[])
{
    cout << Test::count() << endl;
    Test test;
    cout << test.count() << endl;
    return 0;
}


上述代码报错,类的静态成员函数count不能使用volatile关键词进行限定。

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程