的左侧应为结构.或.*,但它是一个结构
structure expected on left side of . or .* but it is a structure
我在chest.contents[0]
上得到了编译错误structure required on left side of . or .*
,但chest
是一个结构:
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;
Chest chest();
并不像你可能相信的那样,调用构造函数
Chest::Chest();
而是函数的声明。创建chest
的正确方法是
Chest chest;
只有当您定义了接受参数的构造函数时,才应该使用括号。
string s;
string s2("Hello");
相关文章:
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 静态结构和一个定义规则
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 在学习数据结构之前对STL有一个了解是好的吗?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 使用完数据结构后清空数据结构是一个好习惯吗?
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何将数据从一个结构链接到另一个结构
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- error dllimport 函数的定义不允许在一个特定的联合中,而其他类、结构和联合将按预期导出
- std::transform 将一个结构数组复制到另一个结构数组
- C++:如何做一个链表,结构作为参数传递?
- 输入 ctrl + x 后如何再次 cin (cin<<x) 循环(cin 一个结构)
- 两个附带的类层次结构-一个好的设计模式