Classes Question

I m trying to learn to use classes, I have tried lots of solutions from internet, but it just wont compile.
(I m using Arduino Uno)
How do you declare a class ?
Can you declare an array of classes and if so ,how ?

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1325.h>

#define OLED_CS 10
#define OLED_RESET 9
#define OLED_DC 8

Adafruit_SSD1325 display(OLED_DC, OLED_RESET, OLED_CS);

const byte width = 128;
const byte height = 64;

Ball ball = Ball();
 
void setup()
{
  display.begin();
  display.clearDisplay();  
  display.drawRect(0, 0, 128, 64, WHITE);
  randomSeed(121);
}

void loop()
{
  ball.update();
  ball.checkBounds();
  ball.draw();
  display.display();
  //delay(100);
}

class Ball
{
  public:
    int oldBallX;
    int oldBallY;
    int ballY;
    int ballX;
    int deltaX;
    int deltaY;

    Ball()
    {
      int oldBallX, oldBallY = 0;
      int ballY = width / 2;
      int ballX = height / 2;
      int deltaX = random(11) - 5;
      int deltaY = random(11) - 5;
    }

    void update()
    {
      oldBallX = ballX;
      oldBallY = ballY;
      ballX += deltaX;
      ballY += deltaY;
    }

    void checkBounds()
    {
      if (ballX > (width - 3))
      {
        ballX = width - 3;
        deltaX *= -1;
      }
      if (ballX < 1)
      {
        ballX = 1;
        deltaX *= -1;
      }
      if (ballY > (height - 3))
      {
        ballY = height - 3;
        deltaY *= -1;
      }
      if (ballY < 1)
      {
        ballY = 1;
        deltaY *= -1;
      }
    }

    void draw()
    {
      display.fillRect(oldBallX, oldBallY, 2, 2, BLACK); // erase old position
      display.fillRect(ballX, ballY, 2, 2, WHITE); // draw current position
    }
};

These are the error messages it gives:

Pong0r:14: error: 'Ball' does not name a type

 Ball ball = Ball();

 ^

 In function 'void loop()':

Pong0r:26: error: 'ball' was not declared in this scope

   ball.update();

   ^

exit status 1
'Ball' does not name a type

Move the declaration of the class above your attempt to use it.

A Class is a type, an array of types probably isn't what you want - I think you mean an
array of instances of a class?

You declare/define a class to define its behaviour, you also declare/define variables holding instances of classes.

In C++ you must declare before use (note that the Arduino IDE goes behind your back to
do that for you for functions, normally you'd have to worry about order of declaration of functions, but
the IDE silently helps you out)

wildbill:
Move the declaration of the class above your attempt to use it.

Thank You, got it working now.

MarkT:
A Class is a type, an array of types probably isn’t what you want - I think you mean an
array of instances of a class?

Yes, I tried to modify the sketch above: (only the modified part shown here)

class Ball
{
 ...
}

Ball ball[3]; 
 
void setup()
{
  display.begin();
  display.clearDisplay();  
  display.drawRect(0, 0, 128, 64, WHITE);
  randomSeed(121);

  for (int i = 0; i < 3; i++)
  {
    ball[i] = Ball(); 
  }

}

Now I´m getting these errors:

Arduino: 1.8.1 

 In function 'void loop()':

Pong0r:92: error: request for member 'update' in 'ball', which is of non-class type 'Ball [2]'

   ball.update();

        ^

Pong0r:93: error: request for member 'checkBounds' in 'ball', which is of non-class type 'Ball [2]'

   ball.checkBounds();

        ^

Pong0r:94: error: request for member 'draw' in 'ball', which is of non-class type 'Ball [2]'

   ball.draw();

        ^

exit status 1
request for member 'update' in 'ball', which is of non-class type 'Ball [2]'

Post your code. Hard to help without it.

However, it looks like you have left out the square brackets and index to indicate which element of the array you're trying to call update on.

    ball[i] = Ball();

You should NEVER be calling constructor directly, without using new, which returns a pointer.

You have ALREADY created the three instances of the Ball class when you did:

Ball ball[3];

When working with classes it’s important to remember that a class declaration is really like a set of blueprints: it tells you how to build the object, but it doesn’t actually define an object you can use in your program. Your Class Ball is the start of your set of blueprints for building a Ball object. However, that blueprint code is a data declaration, not a data definition. That is, you have drawn as set of blueprints that tells the properties and methods that belong to the Ball object. However, just like you can’t “live” inside a set of blueprints, nor can you use your Ball object until you define it. Define and declare are different terms even though they tend to be used interchangeably.

The statement:

Ball ball[3];

takes the blueprint declaration you wrote for the Ball class and uses that model to define storage for three of those objects in memory. In OOP terms, this definition of memory means you have instantiated 3 Ball objects in memory that you can use in your program.

econjack:
When working with classes it's important to remember that a class declaration is really like a set of blueprints: it tells you how to build the object, but it doesn't actually define an object you can use in your program. Your Class Ball is the start of your set of blueprints for building a Ball object. However, that blueprint code is a data declaration, not a data definition. That is, you have drawn as set of blueprints that tells the properties and methods that belong to the Ball object. However, just like you can't "live" inside a set of blueprints, nor can you use your Ball object until you define it. Define and declare are different terms even though they tend to be used interchangeably.

The statement:

Ball ball[3];

takes the blueprint declaration you wrote for the Ball class and uses that model to define storage for three of those objects in memory. In OOP terms, this definition of memory means you have instantiated 3 Ball objects in memory that you can use in your program.

If i could give more karma for this reply, i would, informative and written in a understandable manner

Thanks everybody, got it working now.