0

How to reference one library object in another?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to reference one library object in another?

by shobley on Fri Feb 27, 2009 10:49 pm

I took the NewSoftSerial library and removed all the receive code - then tried to create an instance of this object as a private member of a new library called 'LCDSerial' that drives an LCD over a single pin.

I modified the constructor of the NewSoftSerial library to take a single (TX) pin number, then declared a private attribute in my LCDSerial class as follows:

Code: Select all | TOGGLE FULL SIZE
private:
   NewSoftSerialTX mySerial;


Then in the constructor of LCDSerial I create mySerial as follows:

Code: Select all | TOGGLE FULL SIZE
mySerial = new NewSoftSerialTX(pin);


I get a compile time error about there not being a default constructor for NewSoftSerialTX, even though I'm only calling new with the constructor that takes a pin number.

Code: Select all | TOGGLE FULL SIZE
LCDSerial.cpp: In constructor 'LCDSerial::LCDSerial(uint8_t)':
LCDSerial.cpp:22: error: no matching function for call to 'NewSoftSerialTX::NewS
oftSerialTX()'
hardware\libraries\NewSoftSerialTX/NewSoftSerialTX.h:51: note: candidates are: N
ewSoftSerialTX::NewSoftSerialTX(uint8_t)
hardware\libraries\NewSoftSerialTX/NewSoftSerialTX.h:35: note:                 N
ewSoftSerialTX::NewSoftSerialTX(const NewSoftSerialTX&)
LCDSerial.cpp:24: error: invalid conversion from 'NewSoftSerialTX*' to 'uint8_t'

LCDSerial.cpp:24: error:   initializing argument 1 of 'NewSoftSerialTX::NewSoftS
erialTX(uint8_t)'


The error would indicate that I'm calling a default constructor somewhere, but I'm not.
Is there a trick to using one library object in another?
http://www.stephenhobley.com
http://www.youtube.com/shobley
For all your laser harp, tesla coil, and killer robots from outer space needs.
shobley
 
Posts: 184
Joined: Fri Sep 07, 2007 5:16 pm

Re: How to reference one library object in another?

by nemesisrobot on Sat Feb 28, 2009 9:19 am

new (at least in C++) returns a pointer to the object it created, so you would need to define mySerial as a pointer to NewSoftSerialTX.
nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: How to reference one library object in another?

by shobley on Sat Feb 28, 2009 9:30 pm

Ha! Schoolboy error...

I've been doing C# for too long...

Thanks... Whenever I return to C++ I find I'm writing in a strange dialect that is neither one nor the other.

:D
http://www.stephenhobley.com
http://www.youtube.com/shobley
For all your laser harp, tesla coil, and killer robots from outer space needs.
shobley
 
Posts: 184
Joined: Fri Sep 07, 2007 5:16 pm

Please be positive and constructive with your questions and comments.