Using a class from a separate .cpp is not working

I am struggling how to use a class that I defined in a .cpp file.
I am using Arduino IDE 1.8.15, the sample test project that I have created compile fine but the linker complain.

I have tryed to create a folder in the arduino library folder named it "MyClass" and inside I have put two files: MyClass.h and MyClass.cpp. Then I have created a test.ino sketch where I have instantiated an object of type MyClass and used a class method inside the setup function.

Here is MyClass.h
Here is MyClass.cpp
and this is test.ino

Can you help me to find where is the error? (786 Bytes)

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

You have the declaration of MyClass::MyClass() inside the .h file so it gets declared in both of the places that MyClass.h is included. Change MyClass.h to this:

#ifndef MyClass_h
#define MyClass_h

#include <Arduino.h>

class MyClass
    /* data */
    String Nome;
    MyClass(/* args */)
    void SetNome(String n);

Alternatively, you can move the definition of MyClass::MyClass() to MyClass.cpp. That way it is only defined once.

Mostly it depends on how big the function is. If it is very simple, like in your example, it makes sense to put it in the class declaration. If it were more complex and would clutter up the class declaration, it would make sense to move it to the .cpp file.

I am sorry about the wrong way to open the topic. This is my first time.

Thank you for the clarification.

It was in front of my eyes but I didn't see it.

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