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

  • SEARCH
  • Home
  • 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 8765669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:20:41+00:00 2026-06-13T16:20:41+00:00

I have some code that I wanted to put into a library which uses

  • 0

I have some code that I wanted to put into a library which uses another library, SoftwareSerial. Now I added the SoftwareSerial.h and SoftwareSerial.cpp files to the same folder as the library I’m creating.

My header file looks something like this:

#ifndef MyLibrary_h
#define MyLibrary_h

#include "Arduino.h"
#include "SoftwareSerial.h"

#define MyLibrary_VERSION       1       // software version of this library


//DEFINE ALL CLASS VARIABLES

#define DATA_BUFFER_SIZE 50  //soft serial has 63 byte buffer.

class MyLibrary
{
    public:
        MyLibrary(uint8_t port_in, uint8_t port_out);
        float getSomeValue(uint8_t some_index);
    private:
        SoftwareSerial _serial;
                //Not sure if I should add the constructors below to the above declaration.
                //(uint8_t in_pin=4, uint8_t out_pin=5, bool logic_reversed = false);
        float convertSomeValue(byte upperbyte, byte lowerbyte);
        void flushSerialBuffer();
}; 

#endif

My .cpp file looks like this:

#include "Arduino.h"
#include "MyLibrary.h"
#include "SoftwareSerial.h"


MyLibrary::MyLibrary(uint8_t in_pin, uint8_t out_pin)
{

    bool logic_reversed = false;
    this->_serial(in_pin*, out_pin*, logic_reversed);
        //I tried the declaration below as well.
    //SoftwareSerial _serial(in_pin*, out_pin*, logic_reversed);
}

float MyLibrary::getSomeValue(uint8_t sensor_index) {
    float someValue = 1.1;
    return someValue;
}

float MyLibrary::convertSome(byte upperbyte, byte lowerbyte) {
    float someValue = 0.9;
    return someValue;
}

void MyLibrary::flushSerialBuffer() {
    //Flush serial buffer
    while(_serial.available())
        char c = _serial.read();
}

I would like SoftwareSerial to be a private field in MyLibrary (preferably static but not necessary) but I’ve tried many was of declaring it but nothing seems to work. I keep getting errors like no matching function for call to 'SoftwareSerial::SoftwareSerial() or invalid use of qualified-name 'MyLibrary::_serial'.

I got it to compile fine once by declaring static SoftwareSerial _serial; in my .h file, and SoftwareSerial MyLibrary::_serial(4,5,false); at the top of my .cpp file. The thing is, I would like to set the ports of _serial in my constructor for MyLibrary (so I can create a MyLibrary that uses specific in/out pins for SoftwareSerial) and not have them explicitly declared at the top of the .cpp file.

I’m not so familiar with C coding and Arduino so it would be a great help if someone could explain to me how to declare these properly in the .h file and instanciate them with the correct ports in the MyLibrary constructor or a MyLibrary.begin() function (or something of the like).

Thanks in advance for you helpful comments.

  • 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-06-13T16:20:43+00:00Added an answer on June 13, 2026 at 4:20 pm

    What you need is to make your constructor do the initialization as follows:

    class MyLibrary{
    public:
       MyLibrary(uint8_t, uint8_t);
       //...
    private:
       SoftwareSerial _serial;
       //...
    };
    
    MyLibrary::MyLibrary(uint8_t in, uint8_t out)
       : _serial(in, out)
    {
       //do initialization
    }
    

    This syntax might seem strange at first, but although it isn’t quite as pretty, it clearly differentiates initialization of variables vs operations on variables, which is something that placing the initialization in the body of the constructor can make slightly fuzzy. As a rule, UNLESS you use this syntax to initialize a member variable, C++ will call the default constructor, which will cause a compile error iff the member does not have a callable default constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that shows results in a table Right now it will
I have some code that uses the SQL Server 2005 SMO objects to backup
I have some code that makes heavy use of a thread pool, which I
Forgive me coder for I have sinned. I put some code into a dll
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that causes the box2d physics simulation to stutter forever after
I have some code that runs a model in a loop. Each iteration of
I have some code that looks like this: foreach(var obj in collection) { try

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.