class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX(){return x;}
};
void main()
{
const C c(5);
cout<<c.getX();
system("pause");
}
加上预处理命令 #includes<iostream>。另一个错误是error C2662: 'getX' : cannot convert 'this' pointer from 'const class C' to 'class C &'
常函数只能通过构造函数的参数初始化表对函数进行初始化。
所以改动一: