# Methods defined in .ccp are not defined in sketch

**URL:** <https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482>\
**Category:** Programming\
**Created:** [April 8, 2024, 9:45pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482 "2024-04-08T21:45:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![glennj0158](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/glennj0158/32/1127090_2.png) [@glennj0158](https://forum.arduino.cc/u/glennj0158)\
**Post date:** [April 8, 2024, 9:45pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482/1 "2024-04-08T21:45:16Z")

</div>

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

```arduino
#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

```arduino
// 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

```arduino
// 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:

```arduino
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)

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [April 8, 2024, 10:05pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482/2 "2024-04-08T22:05:39Z")

</div>

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

lib\_classtest.h

```arduino
// 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

```arduino
// 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

```

---

<div class="post-metadata">

**Author:** ![glennj0158](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/glennj0158/32/1127090_2.png) [@glennj0158](https://forum.arduino.cc/u/glennj0158)\
**Post date:** [April 8, 2024, 10:55pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482/3 "2024-04-08T22:55:54Z")

</div>

```arduino
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

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [April 8, 2024, 11:03pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482/4 "2024-04-08T23:03:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glennj0158](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/glennj0158/32/1127090_2.png) [@glennj0158](https://forum.arduino.cc/u/glennj0158)\
**Post date:** [April 8, 2024, 11:13pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482/5 "2024-04-08T23:13:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 5, 2024, 11:13pm UTC](https://forum.arduino.cc/t/methods-defined-in-ccp-are-not-defined-in-sketch/1245482/6 "2024-10-05T23:13:17Z")

</div>

Closed on October 5, 2024, 11:13pm UTC

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