Constructor and its parameter

Hi everyone. Why do I get compilation error for the following code?

class buffer { public: buffer(Serial&); }

Compiler returns: In file included from buffer.cpp:1: buffer.h:5: error: expected `)' before '&' token

Why do I get compilation error for the following code?

Is that all that is in the header file, exactly as it appears in the file?

If so, the ; that ends the class definition is missing. Serial is not a class, it is an instance of the HardwareSerial class, so it can not be used as a type.

I need a reference to Serial port in my class or to NewSoftSerial whichever is supplied by caller. How can I do this?

One guy uses Serial& as a constructor parameter in the attached file. How come?

gsmGPRS.h (5.45 KB)

Serial is an instance of the HardwareSerial class, which derives from Stream.

NewSoftSerial also derives from Stream.

Stream derives from Print, which provides all the output functionality for both HardwareSerial and NewSoftSerial.

If it is just output that you care about, your class can expect an instance of Stream.

For input, it is a bit more complicated. You need two constructors, one taking a HardwareSerial reference, and one taking a NewSoftSerial reference.

In the constructors, you need to set a flag, to hard or soft. Then, when collecting input, you need to check the flag, and use the appropriate reference.

One guy uses Serial& as a constructor parameter in the attached file. How come?

Because he is expecting Serial, Serial1, Serial2, or Serial3 to be specified, on a Mega, rather than Serial or an instance of NewSoftSerial.

PaulS: Because he is expecting Serial, Serial1, Serial2, or Serial3 to be specified, on a Mega, rather than Serial or an instance of NewSoftSerial.

But his code doesn't get compilation error somehow :)

But his code doesn't get compilation error somehow

Time for you to re-read all of reply #1.