constructor problems

This is a discussion on constructor problems within the C++ Programming forums, part of the General Programming Boards category; hiya guys i have what will probably be a trivial problem for you. i have a base class and a ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    constructor problems

    hiya guys i have what will probably be a trivial problem for you.

    i have a base class and a sub class the base constructor takes args but the sub dosent need them here is a little code to help explain my problem
    Code:
    class base
    {
    base(int x,int y);
    
    xPos;
    yPos;
    }
    
    class sub : base
    {
    sub();
    
    xVel;
    yVel;
    }
    
    sub::sub()
    {
     xVel=0;
    yVel=0;
    }
    
    base::base(int x,int y)
    {
    xPos=x;
    yPos=y;
    }
    my problem is how to construct a subclass this is what i have tried but doesnt work yet
    Code:
    sub(int x,int y)  // change sub's constructor to this
    sub::sub(int x,int y) : base(x,y)
    {
    xVel=0;
    yVel=0;
    }
    ok so i know im doing something wrong but as a begginer in lost as to what that is any help would be appreciated thanks in advance

    to clarify this isnt my actual classes but a representation of the real code to make it easier to read
    Last edited by thestien; 09-13-2008 at 01:34 AM. Reason: further explanation

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    it doesnt work as of yet i get a few errors
    subclass error error LNK2019:
    baseclass error error LNK2001:

    if the code above is correct for making a subclass please let me know then i can try to find where my problem is thanks for your help
    sorry i miss read your post.. once i try to build it i get the errors above

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    if the code above is correct for making a subclass please let me know then i can try to find where my problem is thanks for your help
    Your above code is syntactically wrong, so obviously it is not correct. You are on the right track in initialising the base class sub-object in the derived class initialisation list, but you need to specify the types of your member variables (and you probably want to use public inheritance and declare your constructors public).

    Post the smallest and simplest code that demonstrates the problem.
    Last edited by laserlight; 09-13-2008 at 02:14 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ok i have tried to inline the constructor in the subclass decleration and tried also in a seperate .cpp file here are the actual classes if this helps
    Code:
    class Gameobject
    {
    public:  //in Gameobject.h
    Gameobject(int x,int y,int w,int h,int xp,int yp,SDL_Rect cp);
    virtual ~Gameobject();
    SDL_Rect box,clip;
    int xpos,ypos;
    void show(SDL_Surface* src,SDL_Surface* dest);
    
    };
    
    class Ball : Gameobject
    {
    public:   //in Ball.h
    	~Ball();
    	Ball(int x,int y,int w,int h,int xp,int yp,SDL_Rect cp);
    	int xvel;
    	int yvel;
    	void move();
    	void update();
    	
    };
    
    Gameobject::Gameobject(int x, int y, int w, int h,int xp,int yp,SDL_Rect cp)
    {
    	box.x=x;   //in Gameobject.cpp
    	box.y=y;
    	box.w=w;
    	box.h=h;
    	xpos=xp;
    	ypos=yp;
    	clip.h=cp.h;
    	clip.w=cp.w;
    	clip.x=cp.x;
    	clip.y=cp.y;
    }
    
    Ball::Ball(int x,int y,int w,int h,int xp,int yp,SDL_Rect cp):Gameobject(x,y,w,h,xp,yp,cp)
    {
    	xvel=0; // in Ball.cpp
    	yvel=0;
    };
    i hope this helps i have tried a few things but cant get it to compile i am trying to create a Gameobject class that all objects for my game can inherit from i know this is a more complex way to make my game but i feel i can learn alot doing it this way thanks again in advance

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    That's better... but are you sure you want Ball to inherit privately from GameObject?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    at the moment i dont think it will make much difference but i am a newbie at the mo but i still cant get it to compile any ideas whats going wrong?

    here are the errors i get
    Code:
    Ball.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Gameobject::~Gameobject(void)" (??1Gameobject@@UAE@XZ) referenced in function "public: virtual __thiscall Ball::~Ball(void)" (??1Ball@@UAE@XZ)
    Gameobject.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Gameobject::~Gameobject(void)" (??1Gameobject@@UAE@XZ)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    It looks like you forgot to implement the Gameobject destructor.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ah ha it was a trivial problem after all thank you very much for your time i appreciate it
    im sure this wont be my last problem but at least now i can advance as the base class will have at least 4 subclass's i didnt wanna code them all wrong so thanks again

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21