-1

I'm working on a project for a c++ class I'm taking and I can't figure out some compiler errors. My file is supposed to write a base class sprite, that has polymorphic function draw that 4 other classes inherit from. However I can't figure out these compiler issues!

main.cpp: In function ‘int main()’:

main.cpp:72:47: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]

main.cpp:72:47: error: assigning to an array from an initializer list

main.cpp:73:23: error: expected primary-expression before ‘)’ token

main.cpp:74:21: error: expected primary-expression before ‘)’ token

main.cpp:75:22: error: expected primary-expression before ‘)’ token

main.cpp:76:20: error: expected primary-expression before ‘)’ token

main.cpp:81:13: warning: deleting array ‘Sprite array [4]’ [enabled by default]

Here's my code!

#include "drawing.h"
class Sprite
{
  friend class Shadow;
  friend class Speedy;
  friend class Bashful;
  friend class Pokey;
private:
  int row;
  int col;
public:
  int getCol()
  {
    return col;
  }
  int getRow()
  {
    return row;
  }
  void setPosition(int x, int y)
  {
    row = x;
    col = y;
  }
  virtual void draw()
  {
    drawErrorMessage(row, col);
  }
};

class Shadow: public Sprite
{
public:
  virtual void draw()
  {
    drawShadow(row,col);
  }
};
class Speedy: public Sprite
{
public:
  virtual void draw()
  {
    drawSpeedy(row,col);
  }
};

class Bashful: public Sprite
{
public:
  virtual void draw()
  {
    drawBashful(row,col);
  }
};
class Pokey: public Sprite
{
public:
  virtual void draw()
  {
    drawPokey(row,col);
  }
};

int main()
{
  Sprite array[4];
  beginDrawing();
  array = {Shadow(),Speedy(),Bashful(),Pokey()};
  ((Shadow) (array[0]*)).setPosition(5,10);
 ((Speedy)(array[1]*)).setPosition(5,44);
 ((Bashful)(array[2]*)).setPosition(22,44);
 ((Pokey)(array[3]*)).setPosition(22,10);
  array[0].draw();
  array[1].draw();
  array[2].draw();
  array[3].draw();
  delete [] array;
  endDrawing();
}
billz
  • 44,644
  • 9
  • 83
  • 100
CCguy
  • 29
  • 2
  • 8
  • Did you type "assigning to an array from an initializer list" in Google? Seriously, sometimes people don't realize that they can just type an error message of the compiler into Google and land on a page explaining how to solve their problem. Well if you didn't know about that trick, now you know. – sashoalm May 23 '13 at 06:19
  • possible duplicate of [Error: Assigning to an array from an initializer list](http://stackoverflow.com/questions/15603158/error-assigning-to-an-array-from-an-initializer-list) – sashoalm May 23 '13 at 06:20

1 Answers1

1

There are various problems with what you're doing:

1) You're trying to assign an initializer list to an array:

array = {Shadow(),Speedy(),Bashful(),Pokey()};

This is valid C++11, but not valid C++98.

2) Even if you were using C++11, this would be an unfortunate thing to do, because assigning a Shadow to a Sprite will cause slicing (the Sprite sub-object of the Shadow will be assigned, but the rest of the Shadow won't be). See What is object slicing? for additional examples.

3) You're trying to delete[] a stack-allocated array:

Sprite array[4];
//...
delete [] array;

In general, only call delete[] on things you new[]. Simple (irrelevant) example:

int *arr = new int[4];
delete [] arr;

In the context of this question, you're allocating the elements of the array, not the array itself, and it's those you need to delete (using delete and not delete[], since they are allocated with new and not new[]).

4) You're trying to make polymorphic calls on objects whose dynamic type is Sprite. Every element of your array is a Sprite. You probably mean to have an array of Sprite*, whence each element can then point to either a Shadow, or a Speedy, or ... At that point, the dynamic types of the pointed-to objects will be Shadow, Speedy, etc., and making polymorphic calls such as array[i]->draw() will make sense.

5) This syntax is not valid:

((Shadow) (array[0]*)).setPosition(5,10);

I'm assuming you were trying (thinking in terms of the array of pointers below) to do something like:

(*static_cast<Shadow*>(array[0])).setPosition(5,10);

In reality you almost certainly just want:

array[0]->setPosition(5,10);

In general, your code isn't doing quite the right thing. You probably want something more like this:

class Sprite
{
private:
    int col, row;
public:
    virtual ~Sprite() {}

    virtual void draw()
    {
        drawErrorMessage(row, col);
    }

    int getCol() const
    {
        return col;
    }

    int getRow() const
    {
        return row;
    }

    void setPosition(int row_, int col_)
    {
        row = row_;
        col = col_;
    }
};

class Shadow : public Sprite
{
public:
    /*virtual*/ void draw()
    {
        drawShadow(row, col);
    }
};

// etc.

int main()
{
    const int SPRITE_COUNT = 4;
    Sprite *array[SPRITE_COUNT];
    array[0] = new Shadow;
    array[0]->setPosition(5, 10);
    // etc.
    beginDrawing();
    for(int i = 0; i < spriteCount; ++i)
    {
        array[i]->draw();
        delete array[i]; // note: polymorphic deletion, hence the need for a virtual destructor
    }
    endDrawing();
    return 0;
}
Community
  • 1
  • 1
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • But the assignment wants me to: A main function to do the following: Declare an array of 4 pointers to the base Sprite class. Call beginDrawing to prepare the window for drawing. Allocate one instance of each of the derived classes, assigning one of the array's pointers to point to each. So how do I say that the array is 4 in length before and then add things after the call? – CCguy May 23 '13 at 00:28
  • 1
    array can't hold polymorphism. can use vector? otherwise use an array of pointers which point to Sprite type. – billz May 23 '13 at 00:29
  • 1
    Without any mention of slicing, this is a terribly misleading answer. – Ben Voigt May 23 '13 at 00:35
  • @BenVoigt: Fair point, I'll edit it. – Stuart Golodetz May 23 '13 at 00:36