I would like to return a structure from a method but the returned structure needs to be used in a different class. That is my current code:
Song.h:
class song{
public:
Song(char szTitle[50]);
Song();
struct ToneStruct;
ToneStruct GetNextNote();
~Song();
};
Song.cpp:
struct ToneStruct {
char cTone;
int iTime;
int iTimePassed;
};
ToneStruct Song::GetNextNote(){
while (iTempTime != endOfFile) {
//do stuff
while (iTempTime != 59) {
//do stuff
}
ToneStruct toneStruct(cTone,iPlayTime, iTimePassed);
return toneStruct;
}
ToneStruct endStruct('X', 0, 0);
return endStruct;
}
Because of the method I get an error saying "declaration is incompatible with song.h" and on the structs inside the method it says "incomplete type is not allowed". What am I doing wrong?
You have forward declared struct ToneStruct
inside the song
class, which means it is inside the song
namespace and is thus accessible as song::TongStruct
, but you declared a ToneStruct
structure in the source file outside the song
namespace, which will then be assessible as ToneStruct
. The compiler will see them as different types.
To fix the error, you should either move the forward declaration out of the class or use the Song::ToneStruct
for your type.