[SOLVED] Inheritance / Vector Problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
jmpeer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Wed Jul 29, 2009 10:24 pm

[SOLVED] Inheritance / Vector Problem

Post 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?
Last edited by jmpeer on Thu Jul 30, 2009 11:41 am, edited 1 time in total.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Inheritance / Vector Problem

Post by short »

Source code! GO
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
jmpeer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Wed Jul 29, 2009 10:24 pm

Re: Inheritance / Vector Problem

Post 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);
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Inheritance / Vector Problem

Post by Bakkon »

Your vector of Container pointers needs to be static. You only want one instance of that vector.
jmpeer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Wed Jul 29, 2009 10:24 pm

Re: [SOLVED] Inheritance / Vector Problem

Post 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.
Post Reply