0

I'm having troubles getting a 2D class array to get filled with other class objects.

Base class:

class Base {
protected:
    int x, y;
    int moves;
    char name;
    bool occupied;
public:
    Base();
    friend ostream& operator<<(ostream& os, const Base& test);
};

Derived class:

class Derived : public Base {
private:
    const int x = 10;
    const int y = 60;
    Base **array;
public:
    Derived();
    void printBoard();
};

Constructor of Derived class:

Creates the 2d dynamic array

Derived::Derived() {
    array = new Base*[x];
    for (int i = 0; i < x; i++) 
         array[i] = new Base[y];
}

Derived class 2:

class Derived2: public Base{
public:
    Derived2(int x, int y);
};

How do I get the 2D array to accept and afterwards correctly display the objects in that array?

Whenever I try

Derived[x][y] = new Derived2(x,y);

It just doesn't seem to work and I really think I'm stuck on this for a while :(

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
randomehh
  • 27
  • 8

1 Answers1

0

I don't get the relationship between Derived and Base:

  1. Derived is a Base
  2. Derived has an array of Bases

You cannot do Derived[x][y], this requires Derived to have a user-defined operator[] that would return another array (if it was possible to make it static). If you want to access array, you need an instance of Derived and provide an "getter" function for array.

You need to have a 2D array of pointers to do polymorphism: Base ***array;, allocate it like:

array = new Base**[x];
for (int i = 0; i < x; i++) 
     array[i] = new Base*[y];
// this gives you 2D array of uninitialized pointers

And you're forgetting to delete this array in Base's destructor. This is how you do it. Resource management should be the first thing you implement and make sure it's right. Logic follows after that.

This is the "getter" function (public):

Base ***Derived::getArray() const
{
    return array; // array is private, you access it with this function
}

And you use it in the code:

int x = 1, y = 2;
Derived d; // create an instance - this calls Derived's constructor, allocates array

d.getArray()[x][y] = new Derived2(x, y);

I recommend using std::vector (or std::array, since you have constant dimensions) instead of dynamically allocated arrays.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • This is a Turtle vs Hare race. The array of `Base` will contain a few Hares and Turtles. – randomehh Dec 12 '15 at 14:01
  • Thank you, will test this out. Our assignment requires using dynamically allocated arrays, yes, with `std::vector` it would be much easier – randomehh Dec 12 '15 at 14:15
  • Tested, made progress, but weird errors appeared, as `'delete' cannot convert from 'Base' to 'void'` and `delete d.getArray()[x][y];` gets `must be a pointer to complete object type` Also there's a problem with operator= – randomehh Dec 12 '15 at 14:24
  • Wow, this fixed nearly everything, thank you! Just a "getter" function, that returns the `char name`, broke Something about `expression must have a class type` and `left of getName() must have class/struct/union` when trying to retrieve the `name` from the 2D array of pointers. – randomehh Dec 12 '15 at 14:47
  • Show me how you're doing it. Also `char` is a single character name, is that what you want? – LogicStuff Dec 12 '15 at 14:52
  • `char` is just a symbol like `T` for turtle `B` for bush and `H` for hare. the code for "getter" function is `return name;` What I was trying to do was `if(array[x][y].getName() == '?')`in `void printBoard()` function edit: `char getName()` function is located in the `Base` class – randomehh Dec 12 '15 at 14:59
  • `array[x][y]` returns a pointer, you have to do `array[x][y]->getName()`. – LogicStuff Dec 12 '15 at 15:00
  • Thanks, that worked, but hmm, is there any way I could contact you in private? I could message some contact details to you on your youtube account, would really appreciate the help, sorry, so noob at this :/ – randomehh Dec 12 '15 at 15:20
  • Why would you need that? If you're having a problem just post here again. Maybe I'll be around, maybe some other, better people. – LogicStuff Dec 12 '15 at 15:23
  • Okay, I'll keep asking questions on here, thank you a million for the help! :) – randomehh Dec 12 '15 at 15:31
  • Ask a new question about that, I don't really know what it is. Don't forget to post the complete code. – LogicStuff Dec 12 '15 at 15:33