C++, I'm getting an error in creating an array of objects

Started by RetroRain, June 16, 2015, 09:48:17 AM

Previous topic - Next topic

RetroRain

I am programming something in C++, using Allegro as a library.  I'm trying to draw a leveled floor, going straight across, using sixteen 16x16 solid block objects.

So I want to create an array of objects.  My class for the solid block objects is called class wallSolid.  Then, the name of that class for the blocks I wish to create is simply block.  So I initialize the array (outside of main function, at the very beginning of the program), and I am getting some errors.

If you're familiar with C++, could you explain to me what I am doing wrong?

Thanks.

Note: I only posted code that I believe are the problems.  I didn't post the whole program.  So for instance, the draw_floor function was declared in code that I did not show.  I know the code that I didn't show works, as I already tested it.  This array of objects is a new thing I'm trying to do, so I know it is what is causing the problems.


//Declaring my variables, arrays, constants, etc.
wallSolid block[15];

//Declaring the wallSolid class
class wallSolid
   {
      public:
         wallSolid(int xPos, int yPos);
         ~wallSolid();
         int x;
         int y;
   };


//Code that is executed upon creation of an instance of wallSolid
wallSolid::wallSolid(int xPos, int yPos)
   {
      x = xPos;
      y = yPos;
      draw_sprite(screen, solid, x, y);
   }

//Code that is executed upon destruction of an instance of wallSolid
wallSolid::~wallSolid()
   {
   }

//Main function
int main()
   {
      draw_floor();
      readkey();
      return 0;
   }

//Draws floor
void draw_floor()
   {
      int x = 0;
      int y = 192;
      for (int i = 0; i < 16; i++)
         {
            x += 16;
            wallSolid block[i](int x, int y);
         }
   }


These are the errors I'm getting:


4 `wallSolid' does not name a type
In function `void draw_floor()':
109 declaration of `block' as array of functions
[Build Error] Error 1
My YouTube Channel: RetroRainZX85 - https://www.youtube.com/channel/UCdHK6fSwUlcM-q8_EgZQfdw
My Online Store: Adam's Alley - https://adamsalley.com

henke37

Did you forget to include the header file?

Also, it is illegal to attempt to declare a variable while at the same time subscripting it. You have confused declaring and initializing variables.

Bregalad

You should first declare the array, then initialise all its elements. You cannot create an array and call the constructor on all its elements simultaneously, this requires a loop (as far I know, I might be wrong as my knowledge of C++ is quite basic).

KC

The default constructor is called before your program gets control. You don't have one, so that would probably cause issues later. As for fixing that error:
block[i] = wallSolid(x, y);

RetroRain

I appreciate all of the advice you guys have given me, but I'm still not having any luck.

KC, I tried using that piece of code you gave me, and I'm still getting errors.

How do I correct my code so that I can place 16 block objects using a loop?

EDIT - ADDED TO POST

I figured it out as I made another post on a different forum, and the guy helped me with the problems I was having.

Thank you all for your help and your time. :)
My YouTube Channel: RetroRainZX85 - https://www.youtube.com/channel/UCdHK6fSwUlcM-q8_EgZQfdw
My Online Store: Adam's Alley - https://adamsalley.com