Splitting sketch into multiple tabs

I have read a few threads here about how to split a sketch into several files/tabs. So far so good, but the problem is that I cannot get this to work.

I made "dummy" sketch to show this:

The second tab, named "myclass" contain:

class myclass
{
  private
    int   foo;

  public:
    myclass();

    int getFoo();
};

myclass::myclass()
{
  foo = 0;
}

int myclass:getFoo()
{
  return foo;
}

And the main tab contain:

void setup() {
  myclass bar;

}

void loop() {
  // put your main code here, to run repeatedly:

}

When trying to compile this I get:

E:\...\test1.ino: In function 'void setup()':

test1:2:3: error: 'myclass' was not declared in this scope

   myclass bar;

   ^~~~~~~

E:\...\test1.ino:2:3: note: suggested alternative: 'class'

   myclass bar;

   ^~~~~~~

   class

exit status 1
'myclass' was not declared in this scope

I cannot figure out why. Any suggestions what I'm doing wrong?

I extended the test by adding a third tab "func":

int someFunction(const int i)
{
  return 2 + i;
}

Then I also extended the main tab:

class foo {
  private:
    int i;

  public:
    foo();
    int get();
};

foo::foo()
{
  i = 0;
}

int foo::get()
{
  return i;
}

void setup() {
  int i = someFunction(2);
  foo     aa;
  myclass bar;

}

void loop() {
  // put your main code here, to run repeatedly:

}

It seems like I cannot put a class in a second tab. It does does not work, while putting functions in a second tab work and classes can be put in the main tab.

I also tried a forward declaration of myclass in the main tab before the setup() function - no luck.

The second tab, named "myclass"

Has the filename got an extension or is it simply named myclass ?

The file with the class is named myclass.ino. Created by adding a new tab in the editor.

Just found a solution to this. I moved the class definition to myclass.h (not the code for the class) and included it in the main tab. Then it work.

But is this the way it has to be done?

myclass.h:

class myclass
{
  private:
    int   foo;

  public:
    myclass();

    int getFoo();
};

The Arduino-Recommend technique of creating multiple .ino files (i.e. Tabs) is brain-dead, IMO. It just mashes everything into one giant .cpp file in alphabetical order (by filename) following the primary .ino file.

If you want to create modular code, take a look at my Reply #3 Here for a description of techniques more in line with how people who write software for a living do it.

Of course, each .cpp and .h file (as well as the primary .ino file with setup() and loop()) will appear in its own tab.

Since the main .ino goes first, your declaration gets put at the end of the source. Changing the file containing the declaration to .h and including it at the top of the main .ino is the correct way to fix it. As a bonus you can make a myClass.cpp to contain the workings of the class. Then you can put the two myClass files in a myClass library and use it in multiple places. When it moves to a library, change '#include "myClass.h"' to '#include <myClass.h>'

I thought so and at least I found the correct solution. However, I can't figure out how to open the .h file in a tab. Currently I maintain the .h file in Notepad++. But using two different editors for one project is a bit annoying.

If the .h file is in the main sketch folder then it should always open a tab when you open the sketch in the IDE.

The "pro IDE" will let you open arbitrary files but it is still in alpha testing and not suitable for real use.

MorganS:
If the .h file is in the main sketch folder then it should always open a tab when you open the sketch in the IDE.

The "pro IDE" will let you open arbitrary files but it is still in alpha testing and not suitable for real use.

I'm using The Arduino IDE 1.8.10 but I have found a way to open it in the IDE.

Restart the IDE! That was the trick :slight_smile: