C++語言學習(五)——C++語言中的CV限定符錯誤
阿新 • • 發佈:2018-07-15
val ber char 返回 names main code 3.1 類的靜態成員 C++語言學習(五)——C++語言中的CV限定符錯誤
B、靜態成員函數的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-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 <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-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 <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關鍵詞進行限定。
C++語言學習(五)——C++語言中的CV限定符錯誤