I wanted to move some of my code out into a second tab in the Arduino IDE, to keep things better organised. At first, I only tried moving a function, and it seemed to work fine. I could call the function from the setup() function in my main tab, and there were no problems compiling or uploading.

However, I tried putting a whole class into the second tab, and suddenly it didn't work any more. For example:

Tab 1:

TestClass obj;

void setup()
{
    obj.init();
}

void loop()
{
    //...
}

Tab 2:

class TestClass
{
public:
    void init()
    {
        //...
    }
};

When I tried to compile this, it gave me the following errors:

tab1:1: error: 'TestClass' does not name a type
tab1.ino: In function 'void setup()':
tab1:5: error: 'obj' was not declared in this scope

Why does it recognise a function in another tab, but not a class? Is there a way to make it work in the Arduino IDE, or do I need to use an alternative like Eclipse?

  • Is this the entirety of the code? What are the file names? Are there no preprocessor directives like #include? – asheeshr Feb 13 '14 at 16:15
  • Yes, that's the entirety of the code. The tabs are named tab1 and tab2 respectively. The names were set using the IDE, so the underlying files are both .ino. – Peter Bloomfield Feb 13 '14 at 16:21
  • I am starting to hate the jiggery pokery the Arduino IDE does. What it adds in ease of use over AVR it takes away in breaking things like this. – Cybergibbons Mar 10 '14 at 11:11
up vote 12 down vote accepted

It is not possible to declare and use classes declared in one .pde file in another .pde file from within the Arduino IDE.

One workaround is to make the second file into a C++ source file (.cpp) and then add a #include "<filename>" directive in the beginning of the first file.


This code compiles correctly:

Tab 1:

#include "test.cpp"

TestClass obj;

void setup()
{
    obj.init();
}

void loop()
{
    //...
}

test.cpp :

class TestClass
{
public:
    void init()
    {
        //...
    }
};
  • Thanks for the workaround. It works for the basic example, but unfortunately it doesn't work if I add any Arduino-specific calls in the .cpp file. For example, if TestClass::init() tries to call pinMode(), the compiler complains that pinMode was not declared in this scope. Is there a way to access the Arduino functions in my class? – Peter Bloomfield Feb 13 '14 at 16:34
  • 6
    @PeterR.Bloomfield Add a #include <Arduino.h> to the top of the .cpp file. – asheeshr Feb 13 '14 at 16:45
  • That works perfectly. Thanks. – Peter Bloomfield Feb 13 '14 at 16:48
  • 1
    I think it's not good to teach others to include source files (#include "test.cpp"). It works, but may brake the understanding of the linking process. Only header files should be included. – Mars Feb 12 '15 at 1:04
  • 1
    I agree with @Mars - a better solution is to put the class declaration in a .h file, and the definition (implementation) in a .cpp file. So you have two extra files in the project. This is actually the normal way of handling classes. – Nick Gammon Jul 26 '15 at 6:41

The way the Arduino IDE works is that it compiles your code (the code you write in the IDE) as the "main" code. Then it pulls code from all of the libraries you have imported and compiles that along with the main code. To do what you are suggesting would require you to create a library for Arduino.

Here is some more information on Arduino libraries:

http://arduino.cc/en/Guide/Libraries

And here is some on how to create a library:

http://arduino.cc/en/Hacking/LibraryTutorial //this one is the easiest to understand imo
http://playground.arduino.cc/Code/Library
http://www.divilabs.com/2013/03/write-your-own-arduino-library.html#

Here is an example library I wrote https://github.com/jamolnng/Arduino/tree/master/libraries/ShiftRegister as you can see there is no problem with calling Arduino functions from the .cpp file (I know, I've tested the library)

  • 3
    It’s also possible to just create .h and .cpp files in your main project, without having to create a separate library. – microtherion Mar 9 '14 at 15:25

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.