Right so my problem is when declaring Button SoundButtonand Button PlayButton
if i try and use the constructor i get an error.
I was just wondering if there is any way around this ?
You have no the default constructor. So when you are declaring an object of type Button you have to specify all arguments for the constructor that is defined in the class.
For example
1 2 3 4
Title() : PlayButton( /* all arguments that correspond to the parameters of the Button constructor */ ),
SoundButton( /* all arguments that correspond to the parameters of the Button constructor */ )
{
}
Obviously I'll Add the Soundbutton as well, and when I've created the cpp file i will use: Title::Title:PlayButton( 20, 20, 120, 40, "Playbutton.png", MenuButtonClip ){}
After doing some searching ive found a solution which was to define all the functions in the class TitleState.
I am not sure why this solved the problem though and am unsure if it is really solved or will cause me more problems later on ?
If anyone can explain this to me i would be grateful :)
The reason your solution worked, is because of this: TitleState::TitleState:PlayButton(//etc
And for reference, the error: error: found ':' in nested-name-specifier, expected '::'|
It should have been: TitleState::TitleState::PlayButton(//etc
One additional colon is all you needed.
Anyways, your solution works because there was no need for you to write out the scope of your function, which is what caused the previous error due to a missing colon.
Hey,
the problem with the colon was because i hadnt included the parenthesis after TitleState.
If i did what you said that wouldnt compile either.
Thanks for trying to help though :)
I was actually asking why defining all the functions in TitleState stopped the vTable error ?
When you use virtual methods, the compiler has to create a "vtable," so it can match up the overridden methods with their respective class types in the hierarchy.
Is there anyway around this ?
I don't think there is a way around that for primitive types. If the return type is an object pointer, then there's a loop hole you can use.
1 2 3 4 5
Superclass* myotherclass::funky(){
Subclass* ptr= new Subclass();
return ptr;
//Legal because subclass is a derived class of superclass
}
oh well.
Im guessing its the same for parameters as well.
Otherwise i could just pass a bool pointer to it.
Guess ill just have to find a different way of doing what i want.
At least i sorted the vtable situation though :)
Thanks again you've been really helpful :)
You can have functions of the same name but different parameters. It's called overloading. Of course, this means there are two separate functions, and you are not really replacing anything.