C++ 常见错误

#include <iostream>
using namespace std;

class A
{
public:
    A();
    ~A();

    int i = 0;//普通变量不能在类中进行初始化
    /* error: ISO C++ forbids initialization of member ‘i’ */
    static int j = 0;//只有静态常量整形才能在类中初始化,整形包括char short int long及其符号,不能为float、double或其他类型
    /* error: ISO C++ forbids in-class initialization of non-const static member ‘j’ */
    const int k = 0;//常量必须在构造函数的初始化列表中进行初始化
    /* error: ISO C++ forbids initialization of member ‘k’; error: making ‘k’ static */
    const static char *p = "Hello world"; //char *p 不是静态常量整形,所以不能在类中初始化
    /* error: invalid in-class initialization of static data member of non-integral type ‘const char*’ */
     static void fun();
};

A::A()
{

}

A::~A()
{

}

static void fun()
//静态函数只需在类中定义,静态函数定义时不能写static,而且需写成A::fun(),否则会当成一个全局函数
{

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值