0

I'm trying to create an item and box system, where a box can have a flexible amount of items inside it (i.e. not wasting memory by making every box have 50 items, when some will have just a few). I am not quite sure how to do it.

I tried to put the Item struct inside the Box struct as a flexible array, but it doesn't really work.

Here's my code:

struct Item {
    char name[64];
    char slot;
    int weight;
    int size;
    int dmg;
};

struct Box {
    int size;
    Item items[size];
};

int main()
{
    Item sword = { "Sword", 'W', 20, 8, 5};
    Box box = { 3, (sword, sword, sword) };
}
6
  • 1
    Why are you not using std::string and especially std::vector? std::vector<Item> seems to do exactly what you need. Commented May 5, 2024 at 9:17
  • Flexible array members are not standard C++. Which leaves you to figure out what your compiler requires of them and what it doesn't make possible. Commented May 5, 2024 at 9:21
  • I'm sorry, could you be more specific? I'm really new to using vectors and don't really know the difference between a string and char[]. Commented May 5, 2024 at 9:21
  • 1
    @RobinBubeník C++ is complex, you cannot just pick it up as you go along. Find yourself a good book on C++, one that explains how to program effectively. There's really no other way to learn C++ (especially if it is your first language). Commented May 5, 2024 at 9:34
  • You can use dynamic allocation by using a pointer.struct Box {Item* item}; Box.item = new Item[3] Before using vector you must understand how pointers work. Commented May 5, 2024 at 10:12

1 Answer 1

5

A possible solution would be to use std::vector as a dynamic-size container.

You should also use std::string for strings like name (instead of a plain char array). Among other advatages, it will remove the limit of name length of 63 characters you have now.

A minimal example initializing a box with 3 items and then adding 1 item is demonstrated below:

#include <string>
#include <vector>

struct Item {
    std::string name;
    char slot;
    int weight;
    int size;
    int dmg;
};

struct Box {
    std::vector<Item> items;
};

int main()
{
    Item sword = { "Sword", 'W', 20, 8, 5 };
    Box box;
    box.items = { sword, sword, sword }; // initialize with 3 items
    box.items.push_back(sword);          // add another item
}

Notes:

  1. Consider to encapsulate the data fields by making them private and providing accessor methods.
  2. Consider to add constructors for Item and Box, and possibly other methods like add_item for Box.
Sign up to request clarification or add additional context in comments.

14 Comments

I don't totally agree. I think when you're learning c++ you must understand first how char strings and pointers work before using std::string and std:vector. He's learning, he's not writing code in production.
@nikau6 I think there are different opinions regarding this issue. Some believe raw pointers etc. should actually be addressed after using the standard library common classes like string and vector. In any case I wanted to show the direction of a proper solution for this problem. You can post another answer demonstrating the usage of raw arrays for learning purposes.
@nikau6 • I think char strings and pointers (and new and delete) ought to be learned later, as an advanced C++ topic.
@nikau6 • I disagree. Those are advanced C++, not basics. They are often taught as basics, because C++ is often taught as C. With a progression that reflects the chronological order of functional going from C to C++ to C++98 to C++11 to C++14 to C++17 to C++20 to C++23. Which is a disservice to students, because the parts of the language that were added later to make the language easier to learn are the last parts that are taught.
@nikau6 assembly is even further down the foundation "hirarchy". But it doesn't mean you must (or even should) start with it when learning how to program.
|

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.