Using a class with a constructor in another class

How can i use a class with a constructor inside another class ?
E.g
StateMachine.h
1
2
3
4
5
6
7
8
9
class Title:public GameState
{
private:
    Graphics BackGround, Title,Character,Border;
    Sound MenuMusic;
    Button PlayButton;// These classes
    Button SoundButton;
public:
};

Button.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Button
{
private:
    SDL_Rect box;
    SDL_Rect* clip;
    SDL_Rect clips[4];
    Graphics ButtonGraph;
    bool UpDown;

public:
    Button( int x, int y, int w, int h, const char* File, SDL_Rect ButtonClip[4] );
    void HandleEvents(SDL_Event, bool& Action);
    void HandleStickEvents(SDL_Event event, bool& Action);
    void show();
};

Button.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Button::Button( int x, int y, int w, int h, const char* File, SDL_Rect ButtonClip[4] )
{
    //Set the button's attributes
    box.x = x;
    box.y = y;
    box.w = w;
    box.h = h;
    ButtonGraph.LoadImage(File);

    clips[ MOUSEOVER ] = ButtonClip[ MOUSEOVER ];
    clips[ MOUSEOUT ] = ButtonClip[ MOUSEOUT ];
    clips[ MOUSEDOWN ] = ButtonClip[ MOUSEDOWN ];
    clips[ MOUSEUP ] = ButtonClip[ MOUSEUP ];

    clip = &clips[ MOUSEOUT ];
}



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 are trying ... how ? where is your main() ?
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 */ )
{
}

Last edited on
you are trying ... how ?

im not sure what you mean ?
where is your main() ?

i only posted the relevant code otherwise id have to post like 2000 lines of useless(in respect to my question) code :)
Sorry didnt see your reply vlad :)
right so
1
2
3
4
5
6
7
8
9
10
11
class Title:public GameState
{
private:
    Graphics BackGround, Title,Character,Border;
    Sound MenuMusic;
    Button PlayButton;
    Button SoundButton;
public:
    Title:PlayButton( 20, 20, 120, 40, "Playbutton.png", MenuButtonClip ){}
    
};

This is right ?

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 ){}
Okay so that doesn't work :(
i tried
TitleState.cpp
1
2
3
4
5
6
7
8
TitleState::TitleState:PlayButton( 20, 20, 120, 40, "Playbutton.png", MenuButtonClip ),SoundButton(745 ,554 , 45, 35, "SoundButton.png",SoundClip )
{
    BackGround.LoadImage("background4.png");
    Title.LoadImage("title.png");
    Character.LoadImage("CharAnim.png");
    Border.LoadImage("Border.png");
}


and got the errors


C:\Users\Jim\Documents\C++\Muddy Bomber\TitleState.cpp|4|error: found ':' in nested-name-specifier, expected '::'|
C:\Users\Jim\Documents\C++\Muddy Bomber\TitleState.cpp|4|error: expected constructor, destructor, or type conversion before '(' token|
||=== Build finished: 2 errors, 0 warnings ===|



EDIT:OOPS forgot the () After TitleState

now i have this error though

obj\Debug\TitleState.o||In function `ZN10TitleStateC2Ev':|
C:\Users\Jim\Documents\C++\Muddy Bomber\TitleState.cpp|12|undefined reference to `vtable for TitleState'|
||=== Build finished: 1 errors, 0 warnings ===|

Last edited on
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 ?
Last edited on
I've encountered that problem a couple times. Usually it Is because you forgot to define a function you declared.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//myheader.h
class myclass{
   public:
      virtual void funky();
      virtual void funky2();
   private:
      int baz;
};


//myimplementation.cpp
#include "myheader.h"

void myclass::funky(){blah();}



//Whoops no definition for funky2 
Thanks daleth.
I haven't really used virtual classes before so i didn't know if id done something horribly wrong.

Just one more question on virtual functions whilst your here :).

Say i had a class

1
2
3
4
5
6
7
class myclass{
   public:
      virtual void funky();
      virtual void funky2();
   private:
      int baz;
};


and a some subclasses
but in one of the subclasses i needed one of the functions to return a bool or int or whatever
1
2
3
4
5
class SubClass:public myClass{
   public:
       void funky();
      bool funky2();
};


id get a compile error saying conflicting return type or something like that.

Is there anyway around this ?
Sorry if its a stupid question :)
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
}


I've also seen:
1
2
3
4
5
6
struct myclass{
   virtual Superclass* GetNewClass();
};
struct nonrelatedclass: public myclass{
   Subclass* GetNewClass();
};


But I've never tried this before.
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.
Topic archived. No new replies allowed.