简体   繁体   English

结构内部的结构数组,C ++代码

[英]Array of a Structure Inside a Structure, C++ Code

I'm working on a preparing some code for what will ultimately be a MUD; 我正在为最终的MUD准备一些代码。 this is my first 'big' project and I am gradually chiseling out the errors; 这是我的第一个“大”项目,我正在逐步找出错误; however, some problems are now hindering my project and I just can't seem to break them. 但是,现在有些问题阻碍了我的项目,而且我似乎无法打破它们。 Here is my code: 这是我的代码:

#include <iostream>
using namespace std;

int test_var;
#define K    125
#define TEST 50

struct item {
int quantity;
//Some More Stuff Will Be Inside Later//
};

struct inventory {
  struct item[K];  //Error 1 - "expected unqualified-id before '[' token"
} test;

int main()
{
cout << "Number?" << endl;
cin >> test_var;
test.item[TEST].quantity = test_var;  //Error 2 - "'struct inventory' has no member named 'item'"
cout << test.item[TEST].quantity << endl;  //Error 3 - "'struct inventory' has no member named 'item'"
cout << test.item[TEST].quantity;  //Error 4 - "'struct inventory' has no member named 'item'"
return 0;
}

I have to apologize as this code is a bit sloppy, but this represents two things tasks that I'm trying to accomplish. 我很抱歉,因为这段代码有点草率,但这代表了我要完成的两件事。 Number 1, I need some way of having an array of structure 'items' inside the structure 'inventory'. 第一个,我需要某种方式在结构“库存”中具有结构“项目”的数组。 Number 2, I need to insure that I can access the individual elements inside the structures; 第二,我需要确保可以访问结构内部的各个元素; the actual code involves a couple more structures inside structures and it is vital that I can access the individual, non-structure elements (ints, bools, doubles, strings). 实际的代码在结构中涉及更多的结构,因此至关重要的是,我可以访问各个非结构元素(整数,布尔,双精度型,字符串)。 If anyone can offer many any advice on these issues, I would be grateful. 如果有人能在这些问题上提供许多建议,我将不胜感激。 Thank you 谢谢

struct item[K];

You are missing the identifier/name of the object for the struct. 您缺少该结构的对象的标识符/名称。 Notice that item itself is a struct. 注意item本身是一个结构。 So, try 所以,尝试

struct item obj[K];  // struct key word is unnecessary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM