【c++】类的静态成员函数

先看代码

#include <iostream>
using namespace std;

class A
{
public:
    int fun1(int a, int b){return a+ b;}
};

void main()
{
    int a = 1 ,b = 0;
    int c = A::fun1(a,b);
    cout<< c;
    system("pause");
}

编译时报错:

1>------ 已启动生成: 项目: Empty, 配置: Debug Win32 ------
1>正在编译...
1>main.cpp
1>d:\m3_2\sgtest\empty\main.cpp(13) : error C2352: “A::fun1”: 非静态成员函数的非法调用
1>        d:\m3_2\sgtest\empty\main.cpp(7) : 参见“A::fun1”的声明
1>生成日志保存在“file://d:\m3_2\SGTest\Empty\Debug\BuildLog.htm”
1>Empty - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
 说明类的非静态公有方法只能通过对象或指针调用。

猜你喜欢

转载自blog.csdn.net/whueratsjtuer/article/details/51906004