Constructor in derived class

Started by
1 comment, last by matthughson 18 years, 6 months ago
In my game there are going to be boxes. All the data about the box is in this class (right now its only how big it is):

class BoxType: public cSpriteBase {
private:
	int SizeX;
	int SizeY;

public:
	        BoxType()	{SizeX = 0; SizeY = 0;};

	COORD	GetSize();
};

As it is a sprite its derived from the spritebase class, which looks like this:

#ifndef cSpriteBase_CLASS
#define cSpriteBase_CLASS

#include <SDL.h>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <fstream>
#include <windows.h>

#include "Log.h"
#include "SdlWrapper.h"

using namespace std;

struct Animation {
	SDL_Surface* image;			// the actual image with ALL the frames
	vector<int> displayTime;	// for how long should the image be displayed before we go on to the next? the int is the framenumber
};

class cSpriteBase {
private:
	map <string, Animation> Animation;	// this maps holds the animation, call it with the animation name

	int PixelSizeX;					// the size of the cSpriteBase in pixels
	int PixelSizeY;

	int frameTimeStart;			// since which tick has this frame been showing?
	int currentFrame;			// the fram were currently displaying
	string currentAnimation;	// the animation running right now

	SdlWrapper* Sdl;	// a pointer so we can draw with sdlwrapper

public:
			cSpriteBase(string filename);	// automatically load the animations associated with the cSpriteBases name
			~cSpriteBase();

	COORD	GetPixelSize();	// returns the pixel size of the image

	bool	LoadAnimations(string filename);		// load the animations according to the .txt file
	void	SwitchAnimation(string newAnimation);	// switch to another loaded animation

	void	DrawSprite(int PosX, int PosY);	// draws the cSpriteBase at its current position and checks if its time to switch to the next frame
};

#endif

But on the constructor line of the BoxType class I get this: Box.h(22) : error C2512: 'cSpriteBase' : no appropriate default constructor available Must I supply arguments to the parent class from the deriveds class constructor?
Advertisement
Since your cSpriteBase class lacks a default constructor you must supply an argument to its constructor when constructing a BoxType object.

ex:

BoxType() : cSpriteBase("box.file") {SizeX = 0; SizeY = 0;};
Just to expand on what CRane said:

When you create an object that is a derivative (ie. your box class), not only is its constructor called, but so is its parents (this goes for destructors as well. It looks something like this:
Create a new BoxType- cSpriteBase's constructor is called	- BoxType's constructor is calledDelete the BoxType we created	- BoxType's destructor is called- cSpriteBase's destructor is called


So if you look at your code, you're cSpriteBase class's constructor takes a string for a parameter. So when you create your BoxType object, as describe above, the compiler automatically tries to call the cSpriteBase constructor, but it doesn't know what to pass for that string parameter.

So in the code Crane posted, he implicitly tells the cSpriteBase what to use as the string parameter. Alternativly, you could add a default constructor (ie. cSpriteBase();) and it would compile, but probably not do what you want.

Hope I didn't just make that more confusing [wink]

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]

This topic is closed to new replies.

Advertisement