gpt4 book ai didi

c++ - 不支持成员聚合初始值设定项列表?

转载 作者:行者123 更新时间:2023-11-30 02:08:10 25 4
gpt4 key购买 nike

struct bar { int x; };

struct qux
{
static const int foo[3] = { 1, 2, 3 }; // Error

static const bar baz = { 0 }; // Error
};

Visual Studio 2008;标准;

syntax error : '{'

最佳答案

只有整数static 非聚合成员可以在类中初始化。所有其他静态 成员(聚合或其他)必须在类外初始化。

//tag.h

struct tag
{
static const int foo[3]; //aggregate
static const bar baz; //aggregate
static const std::string s; //non-aggregate (non-integral type)
static const int x = 10; //ok : non-aggregate (integral type)
};

//tag.cpp

const int tag::foo[3] = { 1, 2, 3 }; //ok
const bar tag::baz = { 0 }; //ok
const std::string s = "example"; //ok
const int tag::x; //definition - if you want to take its address

关于c++ - 不支持成员聚合初始值设定项列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7194067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com