Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:03:49+00:00 2026-05-25T15:03:49+00:00

I have been reading up a couple of days now about pointers, references and

  • 0

I have been reading up a couple of days now about pointers, references and dereferences in C/C++ targeted for the Arduino and can’t fully udnerstand what I am missing.

I have my sketch which has a setup of

Serial.begin(9600);   //Just for logging.
Serial1.begin(9600);  //Arduino Mega-> Other Device

I use a wrapper class to send BYTE’s over Serial1 by calling a simple function like getStatus().

The issue I am having is I would like to make my class more dynamic, so I would like my class to use Serial1, Serial2, Serial3 or even the base Serial – but I do not know how to build my .h and .cpp files to use these as references.

At the moment my class has a static Serial1 but if somebody would like to use my class they would have to rename everything to Serial if they use Arduino Uno.

In myclass.h I have something like

myClass(HardwareSerial *serial);

and in myClass.cpp (constructor):

myClass::myClass(HardwareSerial &serial) {
     _HardSerial = serial;
 ..
}

But the compiler keeps on moaning about ) expected before & or *.

I have tried various ways and always get the same error- except when I reference my class to the Serial object– but it says Serial was not defined..

I can’t find any tutorials, except Pointer Resource for Arduino.

Declaration and Creation

#include "Print.h"
//datatype* variablename = ⌖
Print* printer = &Serial; Usage

Usage

//this is the equivalent of Serial.print
printer->print("print using the Serial object"); //notice the -> as opposed to .
//change target address (or which address to point to)
printer = &Serial2;
//this is the equivalent of Serial2.print
printer->print("print using the Serial2 object");

Which is exactly what I want – but it seems I do not understand it. I did what they did there, and it does not work for me.

EDIT 1

Thanks, I did it the reference way because it is better yet. I still get these errors, though:

:88: error: 'HardwareSerial' has not been declared

Line 88 from .h:

uint8_t Init(long BaudRate, HardwareSerial& serial);

.h:136: error: ISO C++ forbids declaration of 'HardwareSerial' with no type
.h:136: error: expected ';' before '&' token

Line 136 from .h (this keeps on happening- I don’t know what to use, Serial.write? / Serial?):

private:
HardwareSerial& _HardSerial;

EDIT 2

So, basically because I import the HardWareserial.h file in file myClass.h which is imported in my sketch to use all myClasses stuff – it kills the references in the sketch and wants me to redefine the Serial instances. It seems like the inheritance is the wrong way around…annoying. What’s the default constructor used in the sketch?

It’s like it’s asking me to do this:

HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);

Really? Again I don’t understand…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:03:50+00:00Added an answer on May 25, 2026 at 3:03 pm

    You’re mixing C++ references and pointers. It looks from that example snippet that you should be using pointers, so I have made my answer follow that style. References would probably be a better choice though.

    Your code should look something like this:

    myclass.h

    myClass(HardwareSerial *serial); // ctor
    HardwareSerial * _HardSerial; // member within class
    

    myclass.cpp

    // Constructor takes address of serial port
    myClass::myClass(HardwareSerial *serial) {
     _HardSerial = serial;
    ...
    }
    

    calling code:

    // Gets the address of "Serial1" and passes that to the constructor
    myClass(&Serial1); 
    

    If you wanted to do it as references it would look something like this:

    myclass.h

    myClass(HardwareSerial& serial); // ctor taking reference
    HardwareSerial& _HardSerial; // reference member within class
    

    myclass.cpp

    // Constructor takes reference to a serial port object
    myClass::myClass(HardwareSerial& serial) :
        _HardSerial(serial) // Need to initialise references before body
    {
        ...
    }
    

    calling code:

    myClass(Serial1); // Compiler automatically uses reference
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about entity framework over the past couple of days and
I have been reading for a couple of days about Unity in MVC, and
I have been reading about MVP, MVVM and have used asp.net MVC in couple
I have been digging internet for couple days, reading very old information, that leads
I have been reading a lot about JQuery's deferred object. And I can see
I have been reading about hashcode functions for the past couple of hours and
I just found web2py a couple days ago, and have been reading the documentation
Over the past couple of days I have been reading into using autotools to
I have been reading a couple of articles on stack overflow about aggreation and
Been stuck on this one for a couple days now...I have an existing Liferay

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.