1

I am making several items, and need to set them as being interactable with each other.

Item i1 = Item("Item 1");
Item i2 = Item("Item 2");
Item i3 = Item("Item 3");
Item i4 = Item("Item 4");

i1.setInteractable({i2,i3});
i2.setInteractable({i1,i4});
i3.setInteractable({i1});

This is the method header for the setInteractable method in Item.

void setInteractable(Item i[]);

The interactable items are stored as such:

static Item interactableItems[25];

This doesn't work, the error occurs after the first curly brace. What is the correct way to do this in C++?

5 Answers 5

3

You don't want to store objects in your array (otherwise, they are copied), but references to objects.

Change your storage and interface like that:

void setInteractable(Item& i[]);
static Item& interactableItems[25];

I advise to look in google for:

  • copy constructor
  • pointer vs object vs reference
Sign up to request clarification or add additional context in comments.

Comments

1

C++ doesn't make dynamic lists as easily as you are trying.

To make the dynamic lists you need to first declare a dynamic list:

Item * dlist = new Item[2];

Set them:

dlist[0] = i1;
dlist[1] = i2;

then pass them into your function:

setInteractable(dlist);

Finally you have to remember to clean your memory:

delete [] dlist;

Or... you do this with the standard template library.

std::vector<Item> temp1;

temp1.push_back(i1);
//... other sets

Your function should be:

void setInteractable(std::vector<Item> & list)
{
///
}

The call should be

setInteractable(temp1);

Comments

0

It's a wrong C++ syntax to pass an array initialization like that:

i1.setInteractable({i2,i3}); // error: invalid

In various way you can achieve this. The most straight forward way is to use std::vector. First declare interactableItems as,

static vector<Item> interactableItems;

Now you don't need setInteractable(), if you make above variable public Usage:

i1.interactableItems.push_back(Item("Item 2"));
i1.interactableItems.push_back(Item("Item 3"));

If you want to have variable private then you can just put a wrapper and put the push_back inside it.

2 Comments

See, that would create a new copy of an Item every time he adds it to another Item's interaction list. He needs to pass around a reference (or pointer) for a given Item to every other Item. I'm assuming that if one Item interacts with another, it could potentially affect the other ones too, which this code would fail to do.
I understand that I am making a copy of the item, however, if 2 items interact they are simply removed from an inventory and a new combined item is added. Thus, even if we have copies of the original item, it doesn't really matter (this could be a memory issue, but as this is not a big scale project, it won't really matter).
0

I would solve the problem like so:

Change the method to work on one object at a time, and loop over them:

void setInteractable(Item &i);

for (int i = 0; i < 25; i++)
   for (int j = i + 1; j < 25; j++)
      items[i].setInteractable(items[j]);

Much cleaner to deal with. You can store them in a std::vector inside of the Item, and just with those.

1 Comment

With your solution, you will form a clique of objects. The question does not necessarily want all objects to interact with each other.
0

I would suggest using one of the STL containers. My implementation will proceed thus:

// Method header for the setInteractable method

#include <vector>

class Item {
private:
// some declarations
public:
    void setInteractable(vector<Item &>);
    // some other declarations
};

Your implementation should them selectively push Item objects onto vectors before passing them into the function.

Refer to the Cplusplus Reference Site for some more readings on STL containers.

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.