creating a new Class (brushing up on object oriented...)

Hi all --

I've been wanting to brush up on object oriented programming. When go into tricky topics, I like to do them with cartoon simple topics first.

So I made a purely serial-output program that runs on Arduino, to practice making a new class called Automobile... and manipulate it.

I got the syntax for making classes in Arduino/C++ from this video:

BUT... my simple Automobile class program, when I compile, gives this error:

objectplay:2:1: error: new types may not be defined in a return type
class Automobile {
^

My entire code is in the Pastebin link below... Can you tell me what's wrong? thanks!

Many helpers, and I, will not take a chance by going to another web site to look at code.

A person with 19 posts should have read the locked posts at the top of the forum and should know to use code tags and to properly post.

Hi all -- OK -- here is the full code that gets the error I described above -- thanks --

class Automobile {
  public:
    Automobile(int gaslevel, int velocity, int tanksize);
    void accelerate();
    void halt();
    void decelerate();
    void filltank();
    void consumegas();
    int checkspeed();
    int checkgas();
  private:
    int _gaslevel;
    int _velocity;
    int _tanksize;
}

// creating functions for the object... in other words object members that are METHODS
Automobile::Automobile(int gaslevel, int velocity, int tanksize) {
  _gaslevel = gaslevel;
  _velocity = velocity;
  _tanksize = tanksize;
}

Automobile::accelerate() {
  _velocity++;
}

Automobile::halt() {
  _velocity = 0;
}

Automobile::decelerate() {
  _velocity++;
}

Automobile::filltank() {
  _gaslevel = _tanksize;
}

Automobile::consumegas() {
  gaslevel--;
}

Automobile::checkspeed() {
  return _velocity;
}

Automobile::checkgas() {
  return _gaslevel;
}



void setup() {
  Serial.begin(9600);

  newcar = Automobile(100,0,100);
  oldcar = Automobile(4,45,100);

}

void loop() {
  
  if (random(1,4) == 1) {
    newcar.accelerate();
  }
  
  if (random(1,4) == 2) {
    newcar.decelerate();
  }

  newcar.consumegas();

  if (newcar.checkgas() < 1) {
    newcar.halt();
    newcar.filltank();
  }
  
  Serial.print(" velocity=");
  Serial.print(newcar.checkspeed());
  Serial.print(" gaslevel=");
  Serial.print(newcar.checkgas());
  Serial.println;

}

A 'class' is somewhat like a 'struct' definition on steroids.
That means you have to close the definition with a ';' at the end of the class definition.

The function "void accelerate();" in the class has a return type of "void". That means you have to use "void" as well when you code that function. Do that for the other functions as well.

The private variable _gaslevel is used without underscore.

When you create a variable in setup(), then you can not use it in the loop().
It can be created in the global section.
For example:

int myData(10);   // or int myData = 10;

The same is for the objects.
When you create the objects 'newcar' and 'oldcar' in setup(), the scope of those objects is only in setup().
You can put them in the global section and have them initialized before setup() is executed.
The type of the variable is not "int" but "Automobile".
The variable name is called the "object". The objects are "newcar" and "oldcar".
The value that is assigned to the object is for the constructor.

Automobile newcar(100,0,100);
Automobile oldcar(4,45,100);

void setup() {
  Serial.begin(9600);
}

Serial.println needs to have '(' and ')', it is a function call.

I think you could have done better than this. There are hundreds (thousands ?) examples of Arduino code with a class at Github.

That's great info! Giant thanks for this!