C + + language Learning (v) CV qualifier error in--c++ language? Introduction to CV Qualifier Error 1, CV qualifier Introduction
The CV qualifier is the const and volatile qualifier in the cv-qualifier,c++ language.
In general, there are two cases in the C + + language that cannot be qualified with CV qualifiers:
A, non-member functions cannot use CV-qualification
B, static member functions cannot use CV-qualification
2. Introduction to CV qualifier error messages
CV Qualifier error information in the C + + language such as "Cannot has cv-qualifier", the common CV qualifier error message is as follows:
CV-Qualifier error message for a, non-member function
error: non-member function ‘xxxxxxxxx‘ cannot have cv-qualifier
B, CV-qualifier error information for static member functions
error: static member function ‘static xxxxxxx‘ cannot have cv-qualifier
II. CV-Limited applications for C + + 1, non-member functions
In C + +, non-member functions cannot contain CV-qualifiers, which are const and volatile qualifiers.
#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;}
The above code is an error, and the non-member function sum cannot be qualified with 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;}
The above code is an error, and the non-member function sum cannot be qualified with volatile.
A non-member function cannot use const and volatile to restrict a function, but the return value of a function or function can be qualified with const and 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;}
The friend function of a class is not a member function of a class, and a friend function cannot be restricted with const and 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-qualifierint getValue()const//error{ return Test::data;}int main(int argc, char *argv[]){ cout << Test::count() << endl; Test test; cout << test.count() << endl; return 0;}
The above code error, Class of friend function GetValue cannot use const and volatile to qualify.
2. Static member functions
In C + +, static member functions cannot be CV-qualified, that is, const and 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;}
The above code error, the static member function of the class count cannot be qualified with the Const keyword.
#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;}
The above code error, the static member function of the class count cannot be qualified with volatile keywords.
C + + language Learning (v) CV qualifier errors in--c++ languages