Page 1 of 1

[SOLVED] Inheritance / Vector Problem

Posted: Wed Jul 29, 2009 11:36 pm
by jmpeer
Hello, I have a small problem.

I made a Container class.
A Container class has a vector for other Containers.
A Container class also has a draw function. It draws itself, then all of the members of its vector.
(Containers are drawn as rectangles so I can experiment.)

I can successfully add Containers to Containers and have them drawn.

Then I made a Child Container.
A Child Container is exactly the same, it just calls the parent constructors.
I added a Child Container to a Container's vector, but it will not draw it.
(It will draw everything else though.)

So I think my problem lies in my understanding of inheritance... or possibly vectors.
Any advice?

Re: Inheritance / Vector Problem

Posted: Thu Jul 30, 2009 12:20 am
by short
Source code! GO

Re: Inheritance / Vector Problem

Posted: Thu Jul 30, 2009 12:40 am
by jmpeer
Here's the two classes then. They're not very large.

Code: Select all

#ifndef CONTAINER_H_INCLUDED
#define CONTAINER_H_INCLUDED
#include <vector>
#include <iostream>
class Container
{
    protected:
    SDL_Surface* screen;
    std::vector<Container*> components;

    public:
    Container();
    Container(SDL_Surface* target, int a, int b, int c, int d);
    Container(SDL_Surface* target, int a, int b, int c, int d, int e, int f, int g);
    void draw(Graphics* graphics);
    void add(Container* com);
    Uint32 color;
    SDL_Rect dim; //dimensions
};
Container::Container()
{

}
Container::Container(SDL_Surface* target, int a, int b, int c, int d)
{
    screen = target;
    dim.x = a;
    dim.y = b;
    dim.w = c;
    dim.h = d;
    color = SDL_MapRGB(screen->format,255,255,255);
}
Container::Container(SDL_Surface* target, int a, int b, int c, int d, int e, int f, int g)
{
    screen = target;
    dim.x = a;
    dim.y = b;
    dim.w = c;
    dim.h = d;
    color = SDL_MapRGB(screen->format,e,f,g);
}
void Container::draw(Graphics* graphics)
{
    graphics->setColor(color);
    graphics->drawRect(dim.x,dim.y,dim.w,dim.h);
    for(unsigned int x = 0; x < components.size(); x++)
    {
        components[x]->draw(graphics);
    }
}
void Container::add(Container* com)
{
    components.push_back(com);
}

#endif // CONTAINER_H_INCLUDED

Code: Select all

#ifndef SCREEN_H_INCLUDED
#define SCREEN_H_INCLUDED
class Screen : public Container
{
    protected:

    public:
    Screen(SDL_Surface* target, int a, int b, int c, int d);
    Screen(SDL_Surface* target, int a, int b, int c, int d, int e, int f, int g);
};
Screen::Screen(SDL_Surface* target, int a, int b, int c, int d)
{
    Container(target,a,b,c,d);
}
Screen::Screen(SDL_Surface* target, int a, int b, int c, int d, int e, int f, int g)
{
    Container(target,a,b,c,d,e,f,g);
}

#endif // SCREEN_H_INCLUDED
And this is just from the init

Code: Select all

mainContainer = new Container(screen,0,0,918,676,255,255,255);
testContainer = new Container(screen,0,0,100,100,0,0,0);
testScreen = new Screen(screen,100,0,100,100,0,0,0);
mainContainer->add(testContainer);
mainContainer->add(testScreen);

mainContainer->draw(graphics);

Re: Inheritance / Vector Problem

Posted: Thu Jul 30, 2009 1:36 am
by Bakkon
Your vector of Container pointers needs to be static. You only want one instance of that vector.

Re: [SOLVED] Inheritance / Vector Problem

Posted: Thu Jul 30, 2009 11:46 am
by jmpeer
Alright, I realized my problem.

Code: Select all

Screen::Screen(SDL_Surface* target, int a, int b, int c, int d)
{
    Container(target,a,b,c,d);
}
Called the Container::Container() parent constructor, which explains why it complained if I removed it.

Instead I needed this:

Code: Select all

Screen::Screen(SDL_Surface* target, int a, int b, int c, int d)
: Container(target,a,b,c,d)
{
 
}
This properly inherits from Containers constructor.