0

im currently sitting on a small priv Project(Qt C++) for myself. I want to simulate a garden in console but im struggling already with basics.

Example:

  • Programm starts
  • Programm asks user how big the garden should be
  • User choses 16x16
  • Program creates a Garden with a 16x16 Array where the User can choose where he places plants.

My Code:

#include <QtGlobal>
#include "plant.h"

class Garden {
public:
    Garden(quint16 gardenSize);
private:
    quint16 gardenSize;
    Plant plantarray;
};

-------- ( garden.cpp )

#include "garden.h"
Garden::Garden(quint16 gardenSize) {
    this->gardenSize = gardenSize;
    this->plantarray = new Plant[gardenSize][gardenSize];
}

But im sitting now for over 4 hours at this problem, testing different solutions but nothing is really working :/

i just want to use that array then like:

if (plantarray[x][y] == nullptr){[...]}

etc ...

Sadly the best solutions i've found were from 10 years ago ant wont work anymore ...

Any Suggestions?

Makozer
  • 13
  • 5
  • I see no signs that `Plant plantarray;` is a pointer and thus allowed to accept the address returned by `new`. Making a dynamic 2D array in C++ is a bit tricky. Often you're better off making a 1D array and providing the indexing math to make it look like it's 2D. [Sort of like this](https://stackoverflow.com/a/2076668/4581301) – user4581301 Aug 25 '21 at 16:02

1 Answers1

2

plantarray must be a pointer to pointer:

Plant** plantarray = new Plant*[gardenSize];
for(int i = 0; i < gardenSize; ++i) {
     plantarray[i] = new Plant[gardenSize];
}

But don't forget to delete every pointer in destructor:

for(int i = 0; i < gardenSize; ++i) {
     delete[] plantarray[i];
}
delete[] plantarray;

A better solution would be to use vectors instead:

std::vector<std::vector<Plant>> plantarray(gardensize);
for(auto& plant : plantarray)
{
    plant.resize(gardensize);
}
Hubi
  • 1,629
  • 15
  • 20
  • In addition to deleting the allocations in the destructor you'll also have to observe [the Rule of Three/Five](https://en.cppreference.com/w/cpp/language/rule_of_three) to correctly copy all of the the dynamic allocations. This makes using the `vector` solution even better because `vector` manages all of the allocation, deallocation, and copying for you. – user4581301 Aug 25 '21 at 16:48
  • Well, i ran into new problems ... First Solution: Vector Solution: ''' QVector> plantarray; '''' '''' for(auto& plant : plantarray) { plant.resize(gardensize); } '''' results into: garden.h:25: error: field 'gardenarray' has incomplete type 'QVector >' {aka 'QList >'} QVector> plantarray; ^~~~~~~ – Makozer Aug 25 '21 at 22:01
  • Holy crap, commenting here is crappy ... 5mins edit timewindow ... i do have problems with both solutions ... – Makozer Aug 25 '21 at 22:07
  • As short: both suggested solutions dont work. First has a problem when i wanna do like arr[x][y] = new Plant(); Second doesnt work because "incomplete Type" – Makozer Aug 25 '21 at 23:44
  • Both solutions work. Incomplete Type means your Plant type is probably not implemented or QVector/QList ist not included. – Hubi Aug 26 '21 at 07:35