1

Hello i got a little problem with my C++ project.

First of all i got the class:

class base
{
protected:
    int R, G, B;
public:
    base();
    ~base();
};

and the second class:

class superBase :
    public base
{
public:
    superBase(){R=0; G=0; B=0};
    ~superBase();
};

and the last class which contains the matrix of base class'es:

class gameTable : public gameGraphics
{
private:
    base** table;
public:
    gameTable();
    ~gameTable();
}

When i construct the gameTable class i construct 64 base objects with RANDOM R, G, B values from 0 to 255.

So when the programs goes on, some of mine elemntes in table 'evolves' and becomes superBase's. So here is my problem i don't know how to do that thing. I tried this,

which seems to not working properly.

        superBase newBase;
        table[column][row].~base();
        table[column][row] = newBase;

and the other version:

    table[column][row].~base();
    table[column][row] = new superBase;

My question is how to evolve one element of the table to the superBase class element. As i know it can use the same pointer as base class element.

Greetings and thanks for help!

General_Code
  • 239
  • 1
  • 4
  • 12
  • 1
    where is the definition of `table` ? – Soren Apr 22 '14 at 19:14
  • `new T` returns a pointer. Your "2D array" doesn't hold pointers. Also, don't call the destructors like that. – juanchopanza Apr 22 '14 at 19:15
  • 1
    `table[column][row].~base();` <-- don't call destructors like this. If you are allocating with `new`, you need to `delete`. But you should instead use smart pointers and vectors where possible. – crashmstr Apr 22 '14 at 19:15

1 Answers1

1

“no operator ”=" matches these operands

Here:

table[column][row] = new superBase;

table[a][b] is a base lvalue reference. You are passing it the result of a call to new. This returns pointer to superBase. That assignment can't work. This one would compile

table[column][row] = superBase();

but you would get object slicing. You need to find a way to store (smart) pointers to the base type.

Besides that, your base class needs a virtual destructor. And you shouldn't be calling the destructors directly.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480