Zermish
Zermish

Reputation: 41

Arduino oop error

Im learning how to program an arduous with c++ and am running in to an error that i cant seem to figure out.

Here is my code

class ledControler 
{
  public:
    void ledOn();
    void ledOff();
    void ledStrobe();
    void ledFade();
    ledControler(int);

  private:
    int pinNumber = 0;
};

ledControler::ledControler(int pin)
{
  pinNumber = pin;
  pinMode(pinNumber, OUTPUT);
  ledOn();{
    digitalWrite(pinNumber, HIGH);
  }
  ledOff ();{
    digitalWrite(pinNumber, LOW);
  }
  ledStrobe(int time);{
    digitalWrite(pinNumber, HIGH);
    delay(time);
    digitalWrite(pinNumber, LOW);
    delay(time);
  }
}

and here are the errors:

Object_oriented_LED.ino: In constructor 'ledControler::ledControler(int)':
Object_oriented_LED.ino:24:13: error: expected primary-expression before 'int'
Object_oriented_LED.ino:26:11: error: 'time' was not declared in this scope
Error compiling.

Upvotes: 0

Views: 54

Answers (1)

paxdiablo
paxdiablo

Reputation: 881703

You have methods apparently being created within the constructor, which is not correct. Each should be its own method, such as:

void ledControler::ledOn() {
    digitalWrite(pinNumber, HIGH);
}

Consider your code snippet within the constructor:

ledOn();{
    digitalWrite(pinNumber, HIGH);
}

This is actually being treated as a call to the function ledOn followed by a block containing the digitalWrite call, both of which are perfectly valid (other than the fact there is no ledOn function being defined, of course, something you'd no doubt discover at link time if you could get past the compile errors).

The particular compile error that's preventing this is your attempted definition:

ledStrobe(int time);{ ...

As stated, this is actually more of a call to ledStrobe and unfortunately it's not a valid one since you're not supposed to provide the data types in the call (it would normally be ledStrobe(time);).

So that's where the first error comes from. The second is because, since you're not actually defining a function receiving the time parameter, the code that uses it is complaining that it doesn't exist.

Defining the methods correctly should fix both those issues, and that can be done with something like:

class ledControler {
    public:
        void ledOn();
        void ledOff();
        void ledStrobe();
        void ledFade();
        ledControler(int);
    private:
        int pinNumber = 0;
};

ledControler::ledControler(int pin) {
    pinNumber = pin;
    pinMode(pinNumber, OUTPUT);
}

void ledControler::ledOn() {
    digitalWrite(pinNumber, HIGH);
}

void ledControler::ledOff () {
    digitalWrite(pinNumber, LOW);
}

void ledControler::ledStrobe(int time) {
    digitalWrite(pinNumber, HIGH);
    delay(time);
    digitalWrite(pinNumber, LOW);
    delay(time);
}

Upvotes: 3

Related Questions