Compiler Error after "macroing" Serial...

Hi I have a long working program for an ESP8266 with a large quantity of

Serial.print(), Serial.println() and Serial.printf() codelines.

I wanted to be able to redirect the output to a selectable port.

So I have replaced every occurance of "Serial.print" by "ConOut.print" and begun my program with

#define ConOut "Serial"

(Which should be replaceable later by #define ConOut "Serial1")

Basically the compiling result should be the same, but it already fails with the first option:

request for member 'println' in '"Serial"', which is of non-class type 'const char [7]'

Does someone know, what's going on?

Drop the quotes.

Just an idea… do you literally have quote marks in the # define?

try

define ConOut Serial

and report.

HTH

a7

If you'd posted actual code (in code tags), and actual error messages, we wouldn't have to guess.

Common noob mistake.

wildbill:
Drop the quotes.

Sometimes the solution is too simple and one just need another pair of eyes!
Thank you!

TheMemberFormerlyKnownAsAWOL:
If you'd posted actual code (in code tags), and actual error messages, we wouldn't have to guess.

Common noob mistake.

I did.
Since the relevant part was a single line, I did not consider it to be worth to wrap it in code tags.

Meanwhile I got the help and the prblem is solved.

I did it this way:

// specify a serial port for both line input and application output:
HardwareSerial &console = Serial;
...
      console.println();

This catches semantic and syntax errors better. Also it can be changed dynamically, i.e. after compile time. Not so with a #define.

aarg:
I did it this way:

// specify a serial port for both line input and application output:

HardwareSerial &console = Serial;
...
     console.println();



This catches semantic and syntax errors better. Also it can be **changed dynamically**, i.e. after compile time. Not so with a #define.

I don't think you can change the binding of a Reference dynamically. You can with a pointer. And use the Stream class to be more general:

// specify a serial port for both line input and application output:
Stream *console = &Serial;
...
      console->println();

gfvalvo:
I don't think you can change the binding of a Reference dynamically. You can with a pointer. And use the Stream class to be more general:

That is correct. In c++ you can "bind" a reference exactly once. Once bound, it cannot be changed.

Regards,
Ray L.

gfvalvo:
I don't think you can change the binding of a Reference dynamically. You can with a pointer. And use the Stream class to be more general:

// specify a serial port for both line input and application output:

Stream *console = &Serial;
...
      console->println();

Thanks for that, I made a silly assumption because in my application, I actually don't change it anywhere yet. In fact I was confused because I only recently became aware of references. Some of my existing code might be using pointers where references would work more efficiently. I originally tried using the Stream class but moved on when I couldn't make it work. I'm not even sure why now, but I will go back and apply that suggestion.

One problem which I don't face yet, because I do only use a reference, is that I use Mikal Hart's Streaming library,

which provides the << method of writing output... I suspect it will work (or at least the same way) with the -> operator. My problem is, I'm not very familiar with the ins and outs of Serial instances (the mechanisms behind them). So far, I have just used them in the common, standard ways. I'm still a relatively new OO programmer too.

aarg:
Some of my existing code might be using pointers where references would work more efficiently.

Unlikely, references work with pointers "under the hood".

One problem which I don't face yet, because I do only use a reference, is that I use Mikhal Hart's Streaming library,
PlatformIO Registry
which provides the << method of writing output... I suspect it will work (or at least the same way) with the -> operator.

You'll have to de-reference the pointer:

#include "Streaming.h"

void setup() {
  Stream *ptr;

  Serial.begin(115200);
  delay(1000);
  ptr = &Serial;
  *ptr << "Hello World";
}

void loop() {
}