Go Down

Topic: SoftwareSerial as a private class member (Read 374 times) previous topic - next topic

Raptor5

Why can't I do like this?

class sim300
{
private:
   SoftwareSerial* gsm;
...


and then

sim300::sim300(uint8_t RX, uint8_t TX, uint16_t speed)
{
   gsm = new SoftwareSerial(RX, TX);
   gsm.begin(9600);
}

Compiler error: gsm', which is of non-class type 'SoftwareSerial*'

KeithRB

I don't think you need the pointer operator in the declaration.

softwareSerial gsm; should work fine.

http://arduino.cc/en/Reference/SoftwareSerialConstructor

Raptor5

#2
Nov 16, 2012, 08:38 pm Last Edit: Nov 16, 2012, 08:41 pm by Raptor5 Reason: 1
If I do not use pointer then an object needs to be initialized. But there is no empty constructor for SoftwareSerial.

If I try this

class sim300
{
private:
   SoftwareSerial gsm(2,3);
...

I also get an error

sim300.h : expected identifier before numeric constant

Raptor5

Problem resolved. I had to use -> operator with pointer

sim300::sim300(uint8_t RX, uint8_t TX, uint16_t speed)
{
   gsm = new SoftwareSerial(RX, TX);
   gsm->begin(speed);
}

Go Up