静态成员函数抛出错误

时间:2016-07-04 09:12:33

标签: c++ static-methods

如果我尝试执行以下程序,则表示错误。

#include <iostream>

using namespace std;

class book {
    static int i;
  public :
        static void set_data(void) // static member function
        {
            i++;
            cout<<i<<endl;
        }
};

int book :: i = 0;

int main()
{
    cout<<book :: set_data() << endl; // trying to display the static member function data here but it is giving error
}

如果我们想使用静态成员函数,那么我们是否需要返回静态参数?

1 个答案:

答案 0 :(得分:0)

void具有cout返回类型,因此无法与set_data一起使用。要解决此问题,请std::cout实际返回可以使用static int set_data(void) { i++; return i; } 输出的内容:

static

(您不需要返回&#34;静态参数&#34 ;;此上下文​​中的static表示您无法访问非cout << book::set_data() << endl;类成员数据或函数。)

然后cout << book::i << endl;会好的。或者,写

i

public设置为with text as ( select '63CAGDIRO - VoIP/VDSL - No Service - All' as txt from dual union all select '63CAGDIRO - VoIP/VDSL - No Service - All' from dual union all select '79WIIHAHG - VDSL - Quality Internet - Internet' from dual union all select '79WIIHAHG - VDSL - Quality Internet - Internet' from dual union all select '71GROGRO - VDSL - tv box error code general arg fail - TV' from dual union all select '71GROGRO - VDSL - tv box error code general arg fail - TV' from dual union all select '73LATLWHS - VDSL - No Service All - All services' from dual union all select '73LATLWHS - VDSL - No Service All - All services ' from dual), pos as ( select txt, instr(txt,'-',1,1) as p1, instr(txt,'-',1,2) as p2, instr(txt,'-',1,3) as p3 from text ) select substr(t.txt, 1, p1 -1 ) col_name_1, substr(t.txt, p1 + 1, p2 - p1 - 2 ) col_name_2, substr(t.txt, p2 + 1, p3 - p2 - 2 ) col_name_3, substr(t.txt, p3 + 1, length(t.txt) ) col_name_4 from text t , pos p where t.txt = p.txt; 访问权限。