C++中静态成员函数错误

C++中静态成员函数错误,c++,C++,如果静态类成员和静态类函数具有类作用域,那么为什么我不能访问显示函数它显示错误?如果代替显示功能I写入计数,则显示正确的值,即0 #include <iostream> #include <string> using namespace std; class Person { public: static int Length; static void display() { cout<< ++Len

如果静态类成员和静态类函数具有类作用域,那么为什么我不能访问显示函数它显示错误?如果代替显示功能I写入计数,则显示正确的值,即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;
}
您可以调用display函数,错误是您试图将结果输出到cout。Person::display不会返回任何内容,因此会出现错误

只要改变这个:

cout<< Person :: display(); //error
您可以调用display函数,错误是您试图将结果输出到cout。Person::display不会返回任何内容,因此会出现错误

只要改变这个:

cout<< Person :: display(); //error

如果要将对象管道化到流中,则需要定义适当的运算符如果要将对象管道化到流中,则需要定义适当的运算符,但为什么它与静态成员而不是静态成员函数一起工作?coutBecause Person::Length是一个整数。Person::display不返回任何内容。您不能什么都不输出。但是为什么它可以处理静态成员而不能处理静态成员函数呢?coutBecause Person::Length是一个整数。Person::display不返回任何内容。你不能输出任何东西。
#include <iostream>
#include <string> 

using namespace std;

class Person
{
    public:
     class Displayable {
         template< typename OStream >
         friend OStream& operator<< (OStream& os, Displayable const&) {
             os << ++Person::Length;
             return os;
         }
     };
     static int Length;
     static Displayable display() { return {}; }
};

int Person::Length=0;

int main()
{
   cout<< Person :: display(); //works
   // Person :: Length shows correct value
   return 0;
}