Methods defined in .ccp are not defined in sketch

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)

That's not how you use classes. The following compiles and links with your existing .ino file.

lib_classtest.h

// Library to use in Class Test

class simple {

public:
  simple();
  void add(); 
  void printResult() ;
  void setVal(int, int, int) ;
private:
  int i;
  int j;
  int k;
  int sum;
}; // 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"
#include "lib_classtest.h"

simple::simple() {
  i = 0;
  j = 1;
  k = 2;
}

void simple::add() {
 sum = i + j + k;
}  // End ADD

void simple::printResult() {
 Serial.print(" Sum = ");
 Serial.println(sum);
}  // end PrintResult

void simple::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
public:
  simple();

@van_der_decken Thanks for the response.

I think you did two things:

You added an #include for the .h and

You added a constructor simple(); to the definitions in .h and used it to initialize the private variables i, j, and k.

Is the constructor a part of the solution?

Regards, Glenn

Look again at the .cpp file. Yours was completely different. If all you can see is that I added a constructor... I wash my hands of this.

OK -- a more careful review shows that you removed the repeat of the class definition from the .cpp and restated the functions as members.

I accept that your solution works. I went back and sorted out the tutorial I was using and see where I misunderstood what was said there.

In any event, thanks for your help in correcting my failed understanding.

Regards,
Glenn

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.