Hello C++(五)——C++语言中的CV限定符错误

编程入门 行业动态 更新时间:2024-10-05 03:23:15

Hello C++(五)——C++语言中的CV限定符<a href=https://www.elefans.com/category/jswz/34/1771449.html style=错误"/>

Hello C++(五)——C++语言中的CV限定符错误

 一、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 <iostream>
using namespace std;double sum(double a, double b)const
{return a + b;
}//error: non-member function 'double sum(double, double)' cannot have cv-qualifierint main(int argc, char *argv[])
{double a = 3.14;double b = 2.0;double ret = sum(a, b);return 0;
}

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

#include <iostream>
using namespace std;double sum(double a, double b)volatile
{return a + b;
}//error: non-member function 'double sum(double, double)' cannot have cv-qualifierint 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 <iostream>
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 <iostream>
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 <iostream>
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 <iostream>
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关键词进行限定。

更多推荐

Hello C++(五)——C++语言中的CV限定符错误

本文发布于:2024-02-28 08:03:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   语言   CV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!