结构预计在左侧。或。*,但它是一种结构 [英] structure expected on left side of . or .* but it is a structure

查看:122
本文介绍了结构预计在左侧。或。*,但它是一种结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到编译错误结构需要在左侧。或 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;
}


推荐答案

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

To default-initialize a variable, use

Chest chest;

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

In C++11, this syntax can be used for value-initialization.

Chest chest{};

在C ++ 03中,需要一个复杂的(因为许多编译器错误) Boost库很容易使用:

In C++03, that needs a complicated (because of many compiler bugs) workaround, which the Boost library thankfully has made easy to use:

boost::value_initialized<Chest> chest;

这篇关于结构预计在左侧。或。*,但它是一种结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆