Dividing sketch (classes)

Hi guys,
I did search and find a couple of similar questions; but could not find one with an explained solution (just ‘solved it’ or something along those lines aha).

Trying to divide my (already 600 lines) sketch up, and have a couple of classes and rather hefty functions going with them, that I’d like to move into another file. I was under the impression creating a tab and just dropping them in there should work, although when trying to compile, it appears to not be able to see the class. ( “<CLASS_NAME> does not name a type”)

How would I go about solving this?
Regards.

How would I go about solving this?

Two ways that I can see: 1) Stumble along blindly 2) Post your code here

I know which will be faster.

Buy or download a book about object oriented programming using c++ and actually read it. Google is no substitute for actually learning something properly.

I may be interpreting you incorrectly, but the amount of hostility seems a little much. I've read and practised a reasonable amount of C++ from the ground up.

All I'm asking is how to have a class in a different file, as doing it in the way suggested by other threads appears to throw an error.

I will put some simple code together momentarily, to demonstrate the issue (my actual sketch is way lengthier than needed to demonstrate the issue).

Sorry, but the C++ fundamentals still apply. Creating a class is 100% straight C++. If it ain't finding your class, then you are not doing it right. Did you forget your #includes?

So, in my main tab:

void setup() { sample_class test; }

void loop() {}

and in another tab

class sample_class { };

What includes do I need? This thread seemed to suggest that none were required as the compiler treated them as a single file? Unless I've misinterpreted it: http://forum.arduino.cc/index.php/topic,41039.0.html

Thanks in advance

Actually we are talking straight C here. This applies to every known C compiler on the planet and is not at all unique to Arduino.

Just because you have source code in another tab (i.e file), you have nothing that tells the compiler that it should also attempt to compile code found in those other files. This is a key ingredient. Without this, the compiler will never be aware of any other files.

Let's say you named your file "myclass.h" or whatever. You need the following line of code at the top of your main source file:

include "myclass.h"

That will tell the compiler it should also attempt to compile code found there. The compiler will compile each file into an .obj (object file). There is a final step that will link each of the .obj files into a single binary file which will then get loaded to your Arduino. You don't have to worry about that step as the Arduino IDE does that for you. But you HAVE to tell the compiler about the other files. I recommend you do some review on how to break up C code into separate files. Look at some other code and study how they include the ".c", ".cpp", and ".h" files.

Thanks for your reply, that makes lots of sense. Ironically; it's the way I expected it to work, but I was rather deterred by the thread I've linked above. So you suggest I implement my class(es) as part of a library? (or at least my understanding of one; a .cpp and associated header file).

Thanks again.

You can use the Arduino IDE tabs in two ways (at least).

You can name the files in each tab as NNNN.ino and the compiler will include them automatically in alphabetical order after the principal .ino file (the one with the same name as the Directory). As far as I can see later files can access global variables defined in earlier files.

Or you can name the files NNN.h and NNN.cpp (or anything else that takes you fancy as far as I can see) and then use #include “NNN.h” in the principal file to include them. This has the advantage that you can control the order of inclusion of the files.

…R

This was my understanding; and hence my original question. Although, as above, when trying to create the object from the class in another file, it still can't see it? On the other hand; it doesn't allow for redefinition either?

ie

class sample_class { };

void setup() { }

void loop() {}

class sample_class { };

Throws a 'redefinition of 'class sample_class'' error, as expected. :~

I've worked around the solution through the use of a .cpp and corresponding header file; but at this point am just curious as to why the code in my original post doesn't work? (Assuming the compiler just appends the tabs onto the principal file). Is it because the files are appended at the end of the principal file and hence, the compiler attempts to create the object without the class having been defined (or prototyped)?

Thanks again

Cosford: This was my understanding; and hence my original question. Although, as above, when trying to create the object from the class in another file, it still can't see it? On the other hand; it doesn't allow for redefinition either?

This doesn't go anywhere close to explaining exactly what you tried so how do you expect to get useful advice.

...R

Hence why I have included the sample code and resulting error beneath it; in addition to the sample code I posted previously.

These demonstrate what I have tried in the clearest possible way, no?

Cosford: Hence why I have included the sample code and resulting error beneath it; in addition to the sample code I posted previously.

These demonstrate what I have tried in the clearest possible way, no?

In my opinion NO.

The critical thing is surely how you have named your files and how you have "included" them in your program? You haven't told us any of that.

...R

The principal file and additional tab arenamed simply class_testing.ino and other_class.ino respectively.

I have not used any includes. The code is exactly as shown.

Regards

Try adding a forward declaration of your class in the main tab.

What happens if you rename other_class.ino to other_class.h and insert #include "other_class.h" at the top of the main file?

...R