0

i've trying to make an 2 object which both contains an object array , both are created from the same constructor but when i debug one gets an adress and the other is NULL.

Player::Player(std::string name) {
    this->name = name;
    p.setSize(7);
}

thats the object with the array.

Pile::Pile(int size) {
    stoneArr = new Stone[size];
    this->size = size;
}

Pile::Pile(){

}

thats the object inside of player which consists of an array.

EDIT: https://i.stack.imgur.com/Hr1ZY.jpg

when i deleted the game instance, both are null, if you need like further info let me know i dont mind upload the whole project...

  • 3
    use `std::vector` and include a full working example. – Goswin von Brederlow May 18 '22 at 11:21
  • i cannot use a vector, this is a school project and im forbidden to use it – Shai Susana May 18 '22 at 11:25
  • 1
    Please provide [mcve], we don't have enough information to see the problem. Also, please explain what exactly is `NULL` (that shouldn't be null) – Yksisarvinen May 18 '22 at 11:26
  • You should get used to implement the constructor's initialiser list (not to be confused with `std::initializer_list`): `Player::Player(std::string name) : name(name) { ... }` or actually, to avoid unnecessarily copying internal data, as you accept the string by *value* anyway: `: name(std::move(name))` – this prefers direct initialisation by value over default initialisation and assignment – the latter can have significant overhead for complex types. Moreover, some types (references, const members, non-default-constructable types, ...) *only* can be initialised that way. – Aconcagua May 18 '22 at 11:26
  • @ShaiSusana Then alteast use smart pointers(if they are not forbidden too and not already using). – Jason May 18 '22 at 11:26
  • Is `stoneArr` a smart pointer (`std::unique_ptr`)? Otherwise you will need to implement a destructor to avoid memory leaks – but then the [rule of three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) applies – or in its more modern, optimising form the [rule of five](https://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11) – alternatively on [cppreference](https://en.cppreference.com/w/cpp/language/rule_of_three). – Aconcagua May 18 '22 at 11:30
  • @Aconcagua i used this pointer as the teacher showed in my class *** Stone *stoneArr;*** – Shai Susana May 18 '22 at 11:32
  • @AnoopRana i cant use a smart pointer cxause we havent elarnt it yet. – Shai Susana May 18 '22 at 11:33
  • @ShaiSusana That's an old raw pointer dating before C++11. Replace it with `std::unique_ptr stoneArr;` *if* you are allowed to, otherwise destructor, rule of three, etc, see my previous comment. – Aconcagua May 18 '22 at 11:34
  • @ShaiSusana If you could provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) it will be easier for use to debug the problem and hence provide the solution. – Jason May 18 '22 at 11:35
  • Your default constructor of `Pile` doesn't initialise the members – have you provided initialisers for on declaration? If so, fine, otherwise initialise `size` and `stoneArr` to 0 and nullptr respectively! Side note: Correct type for specifying array lengths and other sizes is `size_t`/`std::size_t`, not `int`. – Aconcagua May 18 '22 at 11:37
  • 1. Don't upload *images*, include code as text directly in the question. 2. Create, as mentioned already, a [mre], which doesn't mean including some entire project with dozens of lines of totally irrelevant code – instead *copy* the project, then strip – step by step – anything that's not contributing to your problem. What *then* remains you can post here (if you still need at all, quite often you'll find the solution to your problem yourself already while creating the MRE...). – Aconcagua May 18 '22 at 11:43

0 Answers0