I created a simple library for formatting text output on the serial monitor. It has thee functions, one that creates carriage returns, one that sets the number of spaces between characters and the last one prints dashes. I followed the tutorial on the Arduino.cc site. The problem I have is that I can’t pass the value to each of the functions when I call them in the code. If you look at the screenshot from the sketch when I create an instance of nFormat it requires me to put a value in and it passes that value to each of the functions (lines, spaces, dashes). I want to be able to specify the value directly when I call the function. See the second red line on the output screenshot. I know this post is a bit long I would appreciate it if someone could point me in the right direction.
nFormat.h
#ifndef nFormat_h
#define nFormat_h
#include "Arduino.h"
class nFormat
{
public:
nFormat(int counter);
void lines();
void spaces();
void dashes();
private:
int _counter;
};
#endif
nFormat.cpp
#include "Arduino.h"
#include "nFormat.h"
nFormat::nFormat(int counter)
{
_counter=counter;
}
void nFormat::lines()
{
for (int x = 0; x < _counter; x++)
{
Serial.println(" ");
}
}
void nFormat::spaces()
{
for (int x = 0; x < _counter; x++)
{
Serial.print(" ");
}
}
void nFormat::dashes()
{
for (int x = 0; x < _counter; x++)
{
Serial.print("-");
}
-
Why are you using a class if you want to be able to pass all the parameters directly to the functions?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2016年05月25日 04:30:44 +00:00Commented May 25, 2016 at 4:30
-
I want to avoid having to make an instance of nFormating everytime I want to change the value of the number of spaces or dashes. For example if I wanted to have 10 dashes instead I would have to create nFormat nFormatTwo(10); I get an error when I compile the program when placing the value inside the function call.Michael Niebauer– Michael Niebauer2016年05月25日 04:35:00 +00:00Commented May 25, 2016 at 4:35
1 Answer 1
If you want to specify the number of spaces/dashes/... every time you call any of these methods, you just have to make that number a parameter of the method:
void lines(int count)
{
for (int x = 0; x < count; x++)
Serial.println();
}
Then, your class does not need its private _counter
. Then, since there
is no data in the class, you do not need to instantiate it: just make
all the methods static
and call the methods as
nFormat::method_name()
. And since you are not making instances, you do
not need a constructor either. And since the methods are really trivial,
there is no point in implementing them in a .cpp file: you can just
inline them in the class declaration. Combining all this gives this small
demo program:
class nFormat
{
public:
static void lines(int count)
{
for (int x = 0; x < count; x++)
Serial.println();
}
static void spaces(int count)
{
for (int x = 0; x < count; x++)
Serial.print(" ");
}
static void dashes(int count)
{
for (int x = 0; x < count; x++)
Serial.print("-");
}
};
void setup()
{
Serial.begin(9600);
nFormat::dashes(5);
nFormat::lines(5);
nFormat::spaces(5);
nFormat::dashes(5);
}
void loop() {}
It could be noted than this class serves only to avoid putting all the
functions in the global namespace. It is essentially a namespace. You
could as well replace class
by namespace
(and remove the public
keyword) to make that more explicit.