1

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;
       }
   }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
lmaw
  • 23
  • 4
  • 6
    What's your question? The compiler error tells it's all! – SergeyA Jan 05 '17 at 19:15
  • 1
    You need to use the ctor's initialization list to initialize the member which does not have a default ctor (in this case `cinder::Serial`) – Borgleader Jan 05 '17 at 19:17
  • *i am still learning cinder and c++* -- The error was an obvious one. Shows that trying to use frameworks to learn C++ isn't such a good idea. – PaulMcKenzie Jan 05 '17 at 19:29

1 Answers1

3

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);  
Christopher Oicles
  • 3,017
  • 16
  • 11