关于static const成员变量在类内赋值的问题

flybywind flybywind 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在C++Primer中提到可以在类内对static const变量赋值,但需要在类外定义。我按照它说的做了,但是却提示:
error C2258: illegal pure syntax, must be '= 0'
error C2252: 'nameSize' : pure specifier can only be specified for functions

// header file
class Account {
// ...
private:
static const int nameSize = 16;
static const char name[nameSize];
};
// text file 这两句话到底该放哪里?文本文件?
const int Account::nameSize; // member definition needed
const char Account::name[nameSize] = "Savings Account";

//main.cpp
#include "stdafx.h"
#include "Account.h"

int main(int argc, char* argv[])
{
Account a;
return 0;
}


...全文

相关推荐

评论 14
收藏 已收藏
分享
返回顶部
flybywind
C++ 语言 65,214 成员 250,514 内容