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?
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 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.