Pages: 1 [2]   Go Down
Author Topic: Why is splitting projects into multiple files so hard?  (Read 3632 times)
0 Members and 1 Guest are viewing this topic.
Southern Ontario
Offline Offline
Sr. Member
****
Karma: 2
Posts: 279
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Southern Ontario
Offline Offline
Sr. Member
****
Karma: 2
Posts: 279
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
extern MyMidi midi(Serial);

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
MyMidi midi(Serial);
To reference that already initialized variable in another file:
Code:
extern MyMidi midi;
Logged

Pages: 1 [2]   Go Up
Jump to: