I'm trying to conditionally setup a Serial connection using "SoftwareSerial" so I wanted to defer creating the object until I know I need it (this differs from all of the examples). So I converted this working example:
#include <SoftwareSerial.h>
SoftwareSerial st_serial = SoftwareSerial(2, 3);
void setup() {
pinMode(2, INPUT);
pinMode(3, OUTPUT);
st_serial.begin(9600);
delay(500);
}
void loop() {
st_serial.print(120, BYTE);
delay(3000);
st_serial.print(0, BYTE);
while(1);
}
To the deferrable version:
SoftwareSerial *st_serial = NULL;
void setup() {
pinMode(2, INPUT);
pinMode(3, OUTPUT);
st_serial = new SoftwareSerial(2, 3);
st_serial->begin(9600);
delay(500);
}
void loop() {
st_serial->print(120, BYTE);
delay(3000);
st_serial->print(0, BYTE);
while(1);
This fails to compile due to "undefined reference to `operator new(unsigned int)'". Although I new to C++ (from C), this appears to be textbook use of C++ and is in many C++ tutorials. I have also avoided the use of "new" since I can't find it mentioned with avr-gcc and instead used both of the following to attempt to get a pointer to a new object.
st_serial = &SoftwareSerial(2, 3);
st_serial = &(SoftwareSerial(2, 3));
Both which compile successfully, but never works (i.e activate the device I communicating with as the first example above does).
So, one, is avg-gcc differ in its implementation of C++ since "new Object" seems standard and doesn't work?, or am I just another newbie. And, two, how do I get the second example creating obj in setup) to work?
Thanks,
/me