Trying to build a simple library.

Trying to build a simple library.
I think I finally got the .h and .cpp files correct.
At least the Arduino IDE is not complaining about them anymore.

The IDE is complaining that the two functions in the library are not in scope.

sketch_Check_for_Prime_Number_v2:10: error: 'refresh_Screen' was not declared in this scope

sketch_Check_for_Prime_Number_v2:13: error: 'getChars' was not declared in this scope

//sketch_Check_for_Prime_Number_v2 -- not accepting all input
#include <stdio.h>
#include <Barry.h>

unsigned long int value; unsigned long int n; unsigned long int c = 2;
void setup()
{
  Serial.begin(115200);
  char s = 's';
  refresh_Screen();
  Serial.println("Enter a number to check if it is a prime\n");
  char v = 'v';
  getChars(); 
  n = value;
  if (value < 2) {
    Serial.print("Not a candidate."); return;
  }
  for ( c = 2 ; c <= n - 1 ; c++ )
  {
    if (n % c == 0)
    {
      Serial.println(String(n) + " is not a prime."); break;
    }
    if ( c == n ) Serial.println(String(n) + " is a prime.");
  }
}
void loop() {}

.h file

/*
  Barry.h - Library of general routines.
  Created by Barry Block, July 22, 2016.
  Released into the public domain for all to ignore.
*/

#ifndef Barry_h
#define Barry_h
#include "Arduino.h"

class Barry
{
  public:
  Barry();
  ~Barry();

  void refresh_Screen(char);
  char s;

  void getChars(char);
  unsigned int long digits; unsigned int long value = 0; char k;
};

#endif

.cpp file

/*
  Barry.cpp - Library of general routines.
  Created by Barry Block, July 22, 2016.
  Released into the public domain for all to ignore.
*/

#include "Arduino.h"
#include "Barry.h"

void Barry::refresh_Screen(char s)
{
    Serial.write(27);    // ESC
    Serial.write("[2J"); // clear screen
    Serial.write(27);    // ESC
    Serial.write("[H");  // cursor to home
    if (s == 's')
     {
      Serial.write(27);    // ESC
      Serial.write("[3J"); // clear scrollback
     }
}

void Barry::getChars(char k)
{
  unsigned long int digits; unsigned long int value = 0; char keypress;
  do
  {
    digits = Serial.available();
  } while (digits < 1); // wait till a key is pressed. 1 == a key press
  if (k == 'k')
   {
     keypress = Serial.read(); keypress = tolower(keypress); 
   }
  do // collect the value if any
    {
      digits = Serial.available();
    } while (digits == -1); // -1 is returned until a key is pressed
  value = Serial.parseInt();
}

Help, please :slight_smile:

Well I see that

 char v = 'v';
should be char k = 'v';

[/code]
But that is another problem.

@flat5:

I might be wrong, but although you did declare to include a local library called Barry, You have not made attempt to call the constructor..

refresh screen is a function.. You called for it, but does not define what it does.

so add this line just above your void setup()

...... something something something ....

Berry barry = Barry();
void setup()
... your code continue

also now change your
refresh_Screen(); to barry.refresh_Screen();

getChars(); to barry.getChars();

hope this help

In your .h

  void refresh_Screen(char);
  char s;

  void getChars(char);
  unsigned int long digits; unsigned int long value = 0; char k;

Are refresh_Screen and getChars missing some brackets?

I see two sets of mistakes.

class Barry
{
  public:
  Barry();
  ~Barry();

You declare a default constructor and a destructor, but do not provide a definition for them in the cpp file. Since you don't declare any other constructors and don't need any special behavior from your default, omit the declaration. The compiler will automatically generate a default constructor that initializes all member variables to their default value. For they integral types in your Barry class, they will all be 0;

Because you do not have any pointers or dynamic storage as class members, you do not need to define a custom destructor either. Omit it.

If you do not declare any contructors, the compiler will create basic versions of a default constructor, copy constructor, copy-assignment operator, and destructor.

  refresh_Screen();
  ...
  getChars();

refresh_Screen and getChars are member functions of the Barry class. It must be used by an object of type Barry. Ahraf's explanation above seems quite off, but the solution in the post is correct. Make those changes.

This makes no sense to me. I've played around with it but no success.

[pre][code]Berry barry = Barry();
void setup()
... your code continue

[/code]

Using Barry.GetChars(); and Barry.refresh_Screen();
result in the errors:
sketch_Check_for_Prime_Number_v2:11: error: expected unqualified-id before '.' token
Barry.refresh_Screen('s');
sketch_Check_for_Prime_Number_v2:14: error: expected unqualified-id before '.' token
Barry.getChars(' ');

flat5:
This makes no sense to me. I've played around with it but no success.

[pre][code]Berry barry = Barry();

void setup()
... your code continue


[/code]


Using Barry.GetChars(); and Barry.refresh_Screen();
result in the errors:
sketch_Check_for_Prime_Number_v2:11: error: expected unqualified-id before '.' token
Barry.refresh_Screen('s');
sketch_Check_for_Prime_Number_v2:14: error: expected unqualified-id before '.' token
Barry.getChars(' ');

Lowerscae "barry".

Lowerscae "barry".

How is that helpful?

flat5:
Lowerscae "barry".

How is that helpful?

Because Barry != barry. The class name is Barry, but the instance is barry.

There is no barry in the header, cpp, or ino.
Barry is the library name, for now. I may change it.
barry is not in any of my code.
Nor is Berry.

Where are you declaring an instance of the Barry class in your sketch?

edit: You can't refer to the class name to call those library functions. You must refer to the instance.

Barry barry; // this is the instance name
char ch = 'a';

barry.getChars(ch);

You're are only confusing me. I do not understand why a lower case barry is approprate or needed..
Did you look at the first post? Can you show me exactly what should be changed/added and where?
Please!

You must declare an instance of your class. It doesn't matter what you call it. I used barry for the instance name. Below I use myInstance as the instance name.

Barry myInstance; // this is the instance name
char ch = 'a';

myInstance.getChars(ch);

Thank you for clarifying. This is what I have tried.

above void Setup() I have added: Barry barry = Barry();

In void Setup() I have: barry.refresh_Screen('s');

I still get: expected unqualified-id before '.' token

One more time: If I remark out #include <Barry.h> I get the same error message!

Tried: Barry barry;
Same error message

Barry.h

/*
  Barry.h - Library of general routines.
  Created by Barry Block, July 22, 2016.
  Released into the public domain for all to ignore.
*/

#ifndef Barry_h
#define Barry_h
#include "Arduino.h"

class Barry
{
  public:
  void refresh_Screen(char);
  char s;

  void getChars(char);
  unsigned int long digits; unsigned int long value = 0; char k;
};

#endif

Barry.cpp. This didn't change.

/*
  Barry.cpp - Library of general routines.
  Created by Barry Block, July 22, 2016.
  Released into the public domain for all to ignore.
*/

#include "Arduino.h"
#include "Barry.h"

void Barry::refresh_Screen(char s)
{
    Serial.write(27);    // ESC
    Serial.write("[2J"); // clear screen
    Serial.write(27);    // ESC
    Serial.write("[H");  // cursor to home
    if (s == 's')
     {
      Serial.write(27);    // ESC
      Serial.write("[3J"); // clear scrollback
     }
}

void Barry::getChars(char k)
{
  unsigned long int digits; unsigned long int value = 0; char keypress;
  do
  {
    digits = Serial.available();
  } while (digits < 1); // wait till a key is pressed. 1 == a key press
  if (k == 'k')
   {
     keypress = Serial.read(); keypress = tolower(keypress);
   }
  do // collect the value if any
    {
      digits = Serial.available();
    } while (digits == -1); // -1 is returned until a key is pressed
  value = Serial.parseInt();
}

The sketch.ino. Whatever you want to call it.

//sketch_Check_for_Prime_Number_v2 -- not accepting all input
#include <stdio.h>
#include <Barry.h>

Barry barry;
char ch;

unsigned long int value; unsigned long int n; unsigned long int c = 2;
void setup()
{
  Serial.begin(115200);
  char s = 's';
  barry.refresh_Screen(ch);
  Serial.println("Enter a number to check if it is a prime\n");
  char v = 'v';
  barry.getChars(ch);
  n = value;
  if (value < 2) {
    Serial.print("Not a candidate."); return;
  }
  for ( c = 2 ; c <= n - 1 ; c++ )
  {
    if (n % c == 0)
    {
      Serial.println(String(n) + " is not a prime."); break;
    }
    if ( c == n ) Serial.println(String(n) + " is a prime.");
  }
}
void loop() {}

Thank you for your help, Tim.
I've made your changes to the .ino.
I did not see any changes to the .h or .cpp files.

I get the same error messages about the "." on the functions lines.

expected unqualified-id before '.' token

.ino fragment

#include <stdio.h>
#include <Barry.h>

Barry barry;
char ch = 's';
unsigned long int value; unsigned long int n; unsigned long int c = 2;

void setup()
{
  Serial.begin(115200);
  //char s = 's';
  barry.refresh_Screen(ch);
  Serial.println("Enter a number to check if it is a prime\n");
  char k = 'v';
  barry.getChars(k); 
  n = value;

I get no errors with those files. Are you certain you have the library in the correct folder?

edit: There was a change to the Barry.h file.

C:\Users\Barry\Documents\Arduino\libraries\Barry

and I restarted the IDE and told it to include the lib...which it did.

But you gave me an idea.
I saved the .ino and told the IDE to "fix encoding and reload".
Errors went bye bye :slight_smile:
Now I'll test and see what happens.
Thank you, Tim, for your patience and help!
I'll be thinking about this now that it works (I hope).

Well all the numbers I input are rejected. I can probably handle that.
Rejection is normal for me :slight_smile:
I'll see what is happening with the variables. Probably use a few print statements to see what is taking place.
Thank you again, all of you!