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 ?
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
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]'
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