user78105: I'm trying to creaper class wraper to use an Streamobject but I've an error when I try to compile.
My .hfile :
#include <Arduino.h>

class TestSerialListener {
public:
  TestSerialListener(Stream &serial);
  void testPrint(void);
private:
  bool _listener_start;
  Stream &_serial;

  void startListener(void);

};

my cppfile
#include "TestSerialListener.h"
#include <Arduino.h>

TestSerialListener::TestSerialListener(Stream &serial) : _serial(serial)
{
}


void TestSerialListener::testPrint(){
    _serial.begin(115200);      <---------- error compilation 
    _serial.println("test");
}


void TestSerialListener::startListener(void)
{
    if(!_listener_start){
        _listener_start = true;
    }
}

And when i try to compile it I got this error : error: 'class Stream' has no member named 'begin'Why I can't use begin()in my classe ?

user42015: the begin() method is defined in the HardwareSerial class, not in Stream. You can look that up yourself in your Arduino installation. For me these files are placed under ~/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/ and are named Stream.h and HardwareSerial.h.
And if you  think about it, this is quite logical. The Stream class can represent any object, that can stream data. It doesn't know anything about how exactly the data is streamed on a hardware level. It just provides the convenient functions for handling the data. The HardwareSerial class then derives from Stream to get those data handling methods and adds its own methods to handle the hardware side of the problem, including setting a baudrate.
When providing a Stream reference to a class constructor you want to begin() the corresponding object outside of the class, before you start to stream any data through it. Doing it this way makes your class widely usable, since you could use any Stream object, even a file on an SD card.
So move the begin() line from your class to the setup() function in your main code.

For reference: Juraj showed the inheritance between the classes in his answer to this question. Have a look at it, since it is quite enlightening.

78105: <p>I'm trying to creaper class wraper to use an <code>Stream</code>object but I've an error when I try to compile.
My <code>.h</code>file :</p>
<pre><code>#include &lt;Arduino.h&gt;

class TestSerialListener {
public:
  TestSerialListener(Stream &amp;serial);
  void testPrint(void);
private:
  bool _listener_start;
  Stream &amp;_serial;

  void startListener(void);

};
</code></pre>
<p>my <code>cpp</code>file</p>
<pre><code>#include &quot;TestSerialListener.h&quot;
#include &lt;Arduino.h&gt;

TestSerialListener::TestSerialListener(Stream &amp;serial) : _serial(serial)
{
}


void TestSerialListener::testPrint(){
    _serial.begin(115200);      &lt;---------- error compilation 
    _serial.println(&quot;test&quot;);
}


void TestSerialListener::startListener(void)
{
    if(!_listener_start){
        _listener_start = true;
    }
}
</code></pre>
<p>And when i try to compile it I got this error : <code>error: 'class Stream' has no member named 'begin'</code>Why I can't use <code>begin()</code>in my classe ?</p>
 42015: <p>the <code>begin()</code> method is defined in the <code>HardwareSerial</code> class, not in <code>Stream</code>. You can look that up yourself in your Arduino installation. For me these files are placed under <code>~/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/</code> and are named <code>Stream.h</code> and <code>HardwareSerial.h</code>.</p>
<p>And if you  think about it, this is quite logical. The <code>Stream</code> class can represent any object, that can stream data. It doesn't know anything about how exactly the data is streamed on a hardware level. It just provides the convenient functions for handling the data. The <code>HardwareSerial</code> class then derives from <code>Stream</code> to get those data handling methods and adds its own methods to handle the hardware side of the problem, including setting a baudrate.</p>
<p>When providing a <code>Stream</code> reference to a class constructor you want to <code>begin()</code> the corresponding object outside of the class, before you start to stream any data through it. Doing it this way makes your class widely usable, since you could use any <code>Stream</code> object, even a file on an SD card.</p>
<p>So move the <code>begin()</code> line from your class to the <code>setup()</code> function in your main code.</p>
<hr />
<p>For reference: Juraj showed the inheritance between the classes in his answer to <a href="https://arduino.stackexchange.com/questions/78719/can-you-use-serial-port-as-a-variable/78725#78725">this question</a>. Have a look at it, since it is quite enlightening.</p>
