加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 预计在左侧的结构.或.*但它是一种结构

发布时间:2020-12-16 10:40:39 所属栏目:百科 来源:网络整理
导读:我得到左侧所需的编译错误结构.或.*在chest.contents [0]上,但胸部是一个结构: class Item {public: int id; int dmg;};class Chest {public: Item contents[10];};int main() { Chest chest(); Item item = chest.contents[0]; return 0;} 解决方法 不,不
我得到左侧所需的编译错误结构.或.*在chest.contents [0]上,但胸部是一个结构:

class Item {
public:
    int id;
    int dmg;
};

class Chest {
public:
    Item contents[10];
};

int main() 
{
    Chest chest();

    Item item = chest.contents[0];

    return 0;
}

解决方法

不,不是,它是一个零参数的函数.

要默认初始化变量,请使用

Chest chest;

在C 11中,此语法可用于值初始化.

Chest chest{};

在C 03中,这需要一个复杂的(因为许多编译器错误)解决方法,感谢Boost库使其易于使用:

boost::value_initialized<Chest> chest;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读