如果静态类成员和静态类函数有类范围,那么为什么我不能访问显示函数(它显示错误)?如果代替显示功能我写入计数它显示正确的值,即0
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
static int Length;
static void display()
{
cout<< ++Length;
}
};
int Person::Length=0;
int main()
{
cout<< Person :: display(); //error
// Person :: Length shows correct value
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以调用该display函数,您的错误是您尝试输出结果cout.Person::display没有返回任何东西,因此错误.
只需改变这个:
cout<< Person :: display(); //error
Run Code Online (Sandbox Code Playgroud)
对此:
Person::display();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
61 次 |
| 最近记录: |