how to create an array of classes

Hey guys,

Basically I am creating a class and defining an X and Y variable within the class. I want to create an array of this class. Think I am going somewhere wrong with the syntax. So in relation to my code I want to create an array code arrayTail of size 10 and type XY.

I get this error the line( XY arrayTail;): expected unqualified-id before ‘[’ token

the problem is located at the bottom of my code.

(overall I am to create a programme that creates a snake or line on an LCD screen that moves just as a snake would e.g. winds, in relation to an analogue stick input)

My code:

#include <SPI.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(3,4,5,6,7); //Initialise display object

int lastX;
int tempX;
int lastY;
int tempY;

// analog
int pin_x = 0;
int pin_y = 1;

int initialLength = 5;

void setup(){

Serial.begin(9600); //Begin Serial Communication
display.setTextSize(1); //Initial Display


void loop(){

// read in analogue stick values
int Ax = analogRead(pin_x);
int Ay = analogRead(pin_y);
// map the analogue values to mmatch that of the pixels in the LCD screen
Ax = map(Ax, 0, 660, 0, 84);
Ay = map(Ay, 0, 660, 0, 84);

//Draw the snake
if(Ax != lastX || Ay != lastY)
// display.drawLine(Ax,Ay,1,BLACK);


class XY
int x;
int y;

class Tail
XY arrayTail;


void initialize()
arrayTail = new XY[10];


Two things:

First, read the two posts by Nick Gammon at the top of this Forum. They tell you how to properly post questions here. Doing so will get you more responses if you follow the rules.

Second, you will get more help if we feel you have tried to solve the issue yourself...which you have not. I Googled "arrays of classes C++" and got 696000 hits. Surely reading some of those will answer your question, plus proving you're will to do some work on your own.

Thanks for your help.
I had done loads of research on my own but searching java arrays of classes stupidly hence why none would work.

First of all, you haven't specified a type for your variable arrayTail which should hold a XY pointer (XY*). However, this is only needed if you want to perform dynamic memory allocation, so instead I would rather suggest you to use an array for this purpose.

XY arrayTail[10]