Making a library

So, I tried making my first Library and this is the error that showed up with verbose on:

Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"





















C:\Users\Dell\AppData\Local\Temp\arduino_modified_sketch_144881\sketch_sep14a.ino: In function 'void loop()':

sketch_sep14a:13:20: error: 'add' was not declared in this scope

   Serial.println ( add ( 5, 2 ) );

                    ^~~

C:\Users\Dell\AppData\Local\Temp\arduino_modified_sketch_144881\sketch_sep14a.ino:13:20: note: suggested alternative: 'rand'

   Serial.println ( add ( 5, 2 ) );

                    ^~~

                    rand

exit status 1

'add' was not declared in this scope

Even though add was clearly declared in the .cpp code below. My library just gives the functions to add, subtract, multiply and divide. Library Name - Operator.

I compressed my Operator.h and Operator .cpp in a ZIP folder and added it to the Arduino libraries list. No errors during the #include<Operator.h> statement.

Operator.h

#ifndef Operator_h
#define Operator_h

#include <Arduino.h>

class Operator 
{
	public:
		int add ( int a, int b );
		int subtract( int a, int b );
		int multiply( int a, int b );
		int divide( int a, int b );
};

#endif

Operator.cpp

#include <Arduino.h>
#include <Operator.h>

int _total;

int add ( int a, int b ) 
{
	_total = a + b;
	return _total;
}

int subtract ( int a, int b )
{
	_total = a - b;
	return _total;
}

int multiply ( int a, int b )
{
	_total = a * b;
	return _total;
}
int divide ( int a, int b )
{
	_total = a / b;
	return _total;
}

Code I tried doing: // The code in the Arduino IDE in which the error showed up.

#include <Operator.h>

void setup() {
  // put your setup code here, to run once:

  Serial.begin ( 9600 );
}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println ( add ( 5, 2 ) );
}

You can see I have declared add() there. Why is the error showing up? What have I done wrong? The zipped folder of this library is attached below. Please help me out. Thanks!

Operator_Library.zip (706 Bytes)

See the library tutorial.

There are a couple of strange things in this code. First is the Morse:: before the name of the function. This says that the function is part of the Morse class. You'll see this again in the other functions in the class.

You need to tell, in the cpp file, who the functions belongs to.

In operator.h, you declare a class named 'Operator' which has an add() function [and others]. In operator.cpp, you define a function named add(). This has nothing to do with the add() function contained within the Operator class.

Since this is a .cpp file, it is compiled separately from your .ino file which means you .ino file has no knowledge of a function called 'add' and hence the error.

Normally, in your .cpp you define the class functions (and don't use _total as a global)

int Operator::add ( int a, int b )
{
	return a + b;
}
...

Your test code will also have to declare an object of type 'Operator' to use the add() function

blh64:
In operator.h, you declare a class named 'Operator' which has an add() function [and others]. In operator.cpp, you define a function named add(). This has nothing to do with the add() function contained within the Operator class.

Since this is a .cpp file, it is compiled separately from your .ino file which means you .ino file has no knowledge of a function called 'add' and hence the error.

Normally, in your .cpp you define the class functions (and don't use _total as a global)

int Operator::add ( int a, int b )

{
return a + b;
}
...



Your test code will also have to declare an object of type 'Operator' to use the add() function

Thank you so much!