Go Down

Topic: Why is splitting projects into multiple files so hard? (Read 4008 times) previous topic - next topic

nickvd

so a single cpp file can only have one class in it? all functions are considered part of the class?

Perhaps I am misunderstanding... I want to split my midi related stuff out of my main sketch into a midi.cpp file... part of this midi related stuff is the class, but not all of it.  Am I to understand that I cannot have a global function and a class in a single cpp file?

PaulS

Quote
so a single cpp file can only have one class in it? all functions are considered part of the class?

No and no. If the function is declared inside a class, it must be implemented as a class method, using the
ReturnType ClassName::MethodName(ArgType arg)
format.

If the function is declared outside the class, it is a global method, but it does not have access to variables defined in other files, unless the extern keyword is used to import the variable into the file scope.

Quote
Perhaps I am misunderstanding... I want to split my midi related stuff out of my main sketch into a midi.cpp file... part of this midi related stuff is the class, but not all of it.  Am I to understand that I cannot have a global function and a class in a single cpp file?

You can have non-class members in the file. But, those non-class members can not call methods in a class without an instance of that class. If the instance is declared in another file, that instance must be imported using the extern keyword.

nickvd

ok... the light bulb is getting brighter...

Code: [Select]
extern MyMidi midi(Serial);

in midi.cpp would take care of things then right?

PaulS

Code: [Select]
extern MyMidi midi(Serial);
Quote
in midi.cpp would take care of things then right?

Close. The variable midi is declared and initialized in one file using
Code: [Select]
MyMidi midi(Serial);
To reference that already initialized variable in another file:
Code: [Select]
extern MyMidi midi;

Go Up