以下C++程序的输出?
null
#include <iostream> class Test { public : void fun(); }; static void Test::fun() { std::cout<< "fun() is static" ; } int main() { Test::fun(); return 0; } |
贡献者 普拉瓦西会面 (A) fun()是静态的 (B) 空屏 (C) 编译错误 答复: (C) 说明: 上述程序编译失败,并显示以下错误消息。 [错误]无法将成员函数“void Test::fun()”声明为具有静态链接[-fpermissive] 在函数“int main()”中: [错误]无法在没有对象的情况下调用成员函数“void Test::fun()
如果要在类外部定义静态函数,则静态关键字必须仅出现在函数声明中,而不是类外部的定义中。
下面的程序现在是正确的。
#include <iostream> class Test { public : static void fun(); }; void Test::fun() { std::cout<< "fun() is static" ; } int main() { Test::fun(); return 0; } |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END