I am trying to create a library, mostly to test my understanding, prior to starting a larger project where libraries will be useful.
I have a basic class (simple) which has no public data and three public methods.
I'm using the approach suggested by several threads where you develop the sketch, move things to a .h and get it running again, then move the methods from the .h to a .cpp file. The approach seems to separate basic code debugging from library debugging.
Everything works correctly as a single sketch and as a sketch + .h
The functions are declared in the class definition in the .h file.
A pointer to additional reading or a suggestion of why I'm blowing up would be appreciated.
Code, libraries, and errors follow
#include "lib_classtest.h"
class simple tst;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // set the baud rate
Serial.println(" -- Begin test program, in Setup");
}
void loop() {
tst.add();
tst.printResult();
tst.setVal(3, -1, -1);
tst.add();
tst.printResult();
Serial.println("All done, long delay");
delay(10000);
} // end of LOOP
lib_classtest.h
// Library to use in Class Test
class simple {
public:
void add();
void printResult() ;
void setVal(int, int, int) ;
}; // end class declaration public variables and methods
lib_classtest.cpp
// This is the CPP file for classtest -- it gets all the methods
#include "Arduino.h"
class simple {
private:
int i = 0;
int j = 1;
int k = 2;
int sum;
public:
void add() {
sum = i + j + k;
} // End ADD
void printResult() {
Serial.print(" Sum = ");
Serial.println(sum);
} // end PrintResult
void setVal(int l = -1, int m = -1, int n = -1) {
if (l == -1) {
Serial.println(" L = -1");
} else {
Serial.println("L <> -1");
i = l;
}
if (m == -1) {
Serial.println(" M = -1");
} else {
Serial.println(" m <> -1");
j = m;
;
}
if (n == -1) {
Serial.println(" N = -1");
} else {
Serial.println(" N <> -1");
k = n;
}
} // End SetVal
}; // End Class
and errors:
C:\Users\glenn\AppData\Local\Temp\ccIJh9P0.ltrans0.ltrans.o: In function `loop':
C:\Users\glenn\OneDrive\Documents\Arduino\ClassAndLibraryTest/ClassAndLibraryTest.ino:14: undefined reference to `simple::add()'
C:\Users\glenn\OneDrive\Documents\Arduino\ClassAndLibraryTest/ClassAndLibraryTest.ino:16: undefined reference to `simple::printResult()'
C:\Users\glenn\OneDrive\Documents\Arduino\ClassAndLibraryTest/ClassAndLibraryTest.ino:18: undefined reference to `simple::setVal(int, int, int)'
C:\Users\glenn\OneDrive\Documents\Arduino\ClassAndLibraryTest/ClassAndLibraryTest.ino:20: undefined reference to `simple::add()'
C:\Users\glenn\OneDrive\Documents\Arduino\ClassAndLibraryTest/ClassAndLibraryTest.ino:21: undefined reference to `simple::printResult()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
All three files are open in the Arduino IDE (2.3.2 which I think is current)