0

I'm working on a preparing some code for what will ultimately be a 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

1
  • The first error in the struct item[k] is that you havent given the item a name. try this: struct item name[k] Commented Jun 20, 2012 at 1:50

1 Answer 1

3
struct item[K];

You are missing the identifier/name of the object for the struct. Notice that item itself is a struct. So, try

struct item obj[K];  // struct key word is unnecessary
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.