Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: expected initializer before...

  1. #1

    expected initializer before...

    Hi everybody!

    I get this error but I can't figure it out:
    "expected initializer before 'MIDIchannel'"



    Code:
    #include "SkyController.h"
    byte MIDIchannel = 1;
    SkyPoti poti(A0, 1);
    
    
    
    void setup() {
    }
    
    void loop() {
      poti.send();
    }
    Your help will be highly appreciated!


    Edit:

    This is SkyController.h:

    Code:
    #ifndef SkyController_h
    #define SkyController_h
    
    #include "SkyPoti.h"
    
    
    #endif
    This is Skypoti.h

    Code:
    #ifndef SkyPoti_h
    #define SkyPoti_h
    
    
    
    #include "Arduino.h"
    #define NOW true
    
    class SkyPoti
    {
      public:
    	SkyPoti(int pin, int number);
        void send(bool now=false);
      private:
    	int _value;
    	int _pin;
    	int _number;
    	int read();
        
    }
    
    
    #endif
    This is Skypoti.cpp

    Code:
    #include "Arduino.h"
    #include "SkyPoti.h"
    
    //Constructor
    SkyPoti::Skypoti(int pin, int number) {
    	pinMode(pin, INPUT);
    	_pin = pin;
    	_value = 0;
    	_number = number;
    }
    
    //reads pin, converts into MIDI value and returns value
    int SkyPoti::read() {
    	return round(analogRead(_pin)/(1023/127));
    }
    
    //sends directly if NOW, otherwise checks for alteration and sends if so
    void SkyPoti::send(bool now) {
    	if (now) {
    	usbMIDI.sendControlChange(_number, _value, MIDIchannel);
    	} else if (this->read()!= _value) {
    	_value = this->read();
    	usbMIDI.sendControlChange(_number, _value, MIDIchannel);
    	}
    }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,262
    You're missing a semicolon ';' after the class definition in Skypoti.h.

    It should be this:

    Code:
    #ifndef SkyPoti_h
    #define SkyPoti_h
    
    
    
    #include "Arduino.h"
    #define NOW true
    
    class SkyPoti
    {
      public:
    	SkyPoti(int pin, int number);
        void send(bool now=false);
      private:
    	int _value;
    	int _pin;
    	int _number;
    	int read();
        
    };  //  <--- added semicolon here
    
    
    #endif

  3. #3
    Senior Member
    Join Date
    Feb 2017
    Posts
    247
    You need a ';' at the end of your class declaration:
    Code:
    class SkyPoti
    {
      public:
    	SkyPoti(int pin, int number);
        void send(bool now=false);
      private:
    	int _value;
    	int _pin;
    	int _number;
    	int read();
        
    };

  4. #4
    Thank you guys!

    After fixing it, this is the next error which I don't understand: error:

    "ISO C++ forbids declaration of 'Skypoti' with no type [-fpermissive]

    SkyPoti::Skypoti(int pin, int number, byte channel)"


    I thought constructors must not have return types?

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    7,612
    Is it a problem with the case of the p .vs. P ? :: SkyPoti::Skypoti(

    And is the code changed to a third param?

    Code:
    class SkyPoti
    {
      public:
    	SkyPoti(int pin, int number);

  6. #6
    Thank you, now it compiles.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •