c++cinder

Constructor for must explicity initialize the member which does not have a default constructor


I'm trying to fix this error, i am still learning cinder and c++. Can someone please help with this. Thank you in advance

Error: "Constructor for 'SerialHandler' must explicity initialize the member 'serial' which does not have a default constructor"

SerialHandler.h

class SerialHandler
{
public :
  SerialHandler(){}; // <- error here
  cinder::Serial serial; // <-
  void setup();
  bool isDone;
  bool isonline;
 };

SerialHandler.cpp

#include "SerialHandler.h"

void SerialHandler::setup()
{
   isDone =true;
   try {
      Serial::Device dev =    Serial::findDeviceByNameContains("cu.usbmodem1411");
      serial.Serial::create( dev, 115200);   
      console() << "Serial Connected" << std::endl;
      isonline =true;
   }
   catch( ... ) {
       console() << "There was an error initializing the serial device!" << std::endl;

       isonline =false;

       const vector<Serial::Device> &devices( Serial::getDevices() );
       for( vector<Serial::Device>::const_iterator deviceIt = devices.begin(); deviceIt != devices.end(); ++deviceIt ) {
       console() << "Device for MAIN?: " << deviceIt->getName() << endl;
       }
   }

}

Solution

  • The problem is a bit less straightforward than one might assume from the error message. cinder::Serial has a protected constructor, so you cannot even have a Serial object as a member of your class. Serial::create is a static member function which returns a SerialRef object (which is a shared pointer to an instance of Serial).

    So your class declaration should have something like:

    class SerialHandler {
             ...
        cinder::SerialRef serial;
             ...
    };
    

    And your create call in SerialHandler::setup() should look like:

    serial = cinder::Serial::create( dev, 115200);