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

Thread: object reference (I'm probably doing it wrong)

  1. #1

    object reference (I'm probably doing it wrong)

    Hi everybody!

    I'm trying to pass an object by reference to another object but I'm afraid I somehow didn't get, how to do it.

    Error message:

    Arduino: 1.8.8 (Windows 10), TD: 1.45, Board: "Teensy 3.5, MIDI, 120 MHz, Faster, US English"

    Miditest2:6: error: no matching function for call to 'SkyFlash::SkyFlash(SkyPoti*, int)'
    SkyFlash flash1(&poti1, 0);

    ^

    In file included from */SkyController.h:5:0,

    from *\Miditest2.ino:1:

    */SkyFlash.h:12:2: note: candidate: SkyFlash::SkyFlash(SkyPoti&, int)

    SkyFlash(SkyPoti& Poti, int pin);

    ^

    */SkyFlash.h:12:2: note: no known conversion for argument 1 from 'SkyPoti*' to 'SkyPoti&'

    */SkyFlash.h:10:7: note: candidate: constexpr SkyFlash::SkyFlash(const SkyFlash&)

    class SkyFlash {

    ^

    */SkyFlash.h:10:7: note: candidate expects 1 argument, 2 provided

    */SkyFlash.h:10:7: note: candidate: constexpr SkyFlash::SkyFlash(SkyFlash&&)

    */SkyFlash.h:10:7: note: candidate expects 1 argument, 2 provided

    no matching function for call to 'SkyFlash::SkyFlash(SkyPoti*, int)'

    Code:
    Code:
    #include "SkyController.h"
    byte MIDIchannel = 1;
    SkyPoti poti1(A0, 1, MIDIchannel);
    SkyPoti poti2(A1, 2, MIDIchannel);
    
    SkyFlash flash1(&poti1, 0);
    
    
    
    void setup() {
    }
    
    void loop() {
      poti1.send();
      poti2.send();
      flash1.send();
    }
    SkyPoti.h:
    Code:
    #ifndef SkyPoti_h
    #define SkyPoti_h
    
    
    
    #include "Arduino.h"
    
    
    class SkyPoti
    {
      public:
    	SkyPoti(int pin, int number, byte channel);
        void send();
    	void flash(bool on);
    	bool flashed;
    	int number;
      private:
    	int _value;
    	int _pin;
    	int _newvalue;
    	int read();
    	int _newread;
    	int _divider;
    	byte _channel;
        
    };
    
    
    #endif
    SkyPoti.cpp:
    Code:
    #include "Arduino.h"
    #include "SkyPoti.h"
    
    //Constructor
    SkyPoti::SkyPoti(int pin, int number, byte channel) {
    	pinMode(pin, INPUT);
    	_pin = pin;
    	_value = 0;
    	number = number;
    	_channel = channel;
    	_newread;
    	_divider =  1023/127;
    	flashed = false;		
    }
    
    //reads pin, averages signal, converts into MIDI value and returns value
    int SkyPoti::read() {
    
    	_newread = analogRead(_pin);
    	if (_newread < _divider/2) {
    		return 0;
    	} else if (_newread % _divider == 0) {
    		return _newread / _divider;
    	} else {
    		return _value;
    	}
    }
    
    //checks for modulation, registers it and sends it if flashbutton is not active
    void SkyPoti::send() {
    	_newvalue = read();
    	if (_newvalue != _value) {
    		if (!flashed) {
    			usbMIDI.sendControlChange(number, _newvalue, _channel);
    		}
    	_value = _newvalue;
    	}
    }
    
    //flashbutton
    void SkyPoti::flash(on) {
    	if (on) {
    		flashed = true;
    		usbMIDI.sendControlChange(number, 127, _channel);
    	} else {
    		flashed = false;
    		usbMIDI.sendControlChange(number, _value, _channel);
    	}
    }
    SkyFlash.h:
    Code:
    #ifndef SkyFlash_h
    #define SkyFlash_h
    
    
    
    #include "Arduino.h"
    #include "SkyPoti.h"
    
    
    class SkyFlash {
      public:
    	SkyFlash(SkyPoti& Poti, int pin);
        void send();
      private:
    	SkyPoti& poti;
    
        
    };
    
    
    #endif
    SkyFlash.cpp:
    Code:
    #include "Arduino.h"
    #include "SkyFlash.h"
    #include "SkyPoti.h"
    #include "Bounce.h"
    
    //Constructor
    SkyFlash::SkyFlash(SkyPoti& Poti, int pin) : Bounce(pin, 10) {
    	poti = Poti;
    	pinMode(pin, INPUT_PULLUP);
    	
    }
    
    //flash that thing!
    void SkyFlash::send() {
    	Bounce::update();
    	if (Bounce::fallingEdge()) {
    		poti->flash(true);
    	}	else  if (Bounce::risingEdge()) {
    		poti->flash(false);
    	}
    }
    I hope it is readable. Thank you in Advance!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,734
    Your first file includes SkyController.h, which isn't any of the other 4 files you posted.

    Might be best to put all the files into a ZIP archive and attach that to your message. If using the forum's "Quick Reply", click the "Go Advanced" button to get the full editor that lets you attach a ZIP file to your message.

  3. #3
    Quote Originally Posted by PaulStoffregen View Post

    Might be best to put all the files into a ZIP archive and attach that to your message.
    Thank you, here it is.
    Attached Files Attached Files

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,734
    About your original question, aside from a mix of files that clearly don't go together, I see 3 problems.

    1: References have to be initialized in the constructor declaration. You can't assign them inside the constructor.

    Code:
    //Constructor
    SkyFlash::SkyFlash(SkyPoti& Poti, int pin) : poti(Poti), bounce(pin, 10) {  // must initialize
    	//poti = Poti; <-- assignment isn't legal
    	pinMode(pin, INPUT_PULLUP);
    }
    You can see where I've marked these in bold. C++ references are special. They must be initialized when created, and then can never be changed by assignment. So you must initialize in the constructor declaration.

    2: For the Bounce object, you need to create it inside your class. The initializer only calls its constructor. So you can't create it like this:

    Code:
    SkyFlash::SkyFlash(SkyPoti& Poti, int pin) : Bounce(pin, 10) {
    Instead, you create it within your class, like this:

    Code:
    class SkyFlash {
      public:
        SkyFlash(SkyPoti& Poti, int pin);
        void send();
      private:
        SkyPoti& poti;
        Bounce bounce;
    };
    Then in your constructor, you cause its constructor to be called with the initializer list.

    Code:
    SkyFlash::SkyFlash(SkyPoti& Poti, int pin) : poti(Poti), bounce(pin, 10) {
    3: In the class instance in your main sketch, you don't use '&' for an input which takes a reference. That's only done for pointer inputs.

    So you would change this:

    Code:
    SkyFlash flash1(&poti1, 0);
    to just this:

    Code:
    SkyFlash flash1(poti1, 0);
    I'll attach a ZIP file to this message with a trimmed copy of your code which does compile without any errors. Hopefully that helps. And hopefully you can see ZIP files can be attached to messages here. When you're showing code with several files, best to do it this way, and make sure your ZIP file is a complete set of files that are meant to be used together. Copying several individual files is an error-prone process... and it's entirely possible I missed some other detail you'll need, because I had to delete lots of stuff and then lightly edit your files just to create this answer for you.
    Attached Files Attached Files

  5. #5
    Thank you Paul, that helped a lot! There were a few more things but I was able to figure them out myself. Now it works!

Posting Permissions

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