Jump to content
  • Advertisement
Sign in to follow this  

Constructor in derived class

This topic is 4793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 {
	int SizeX;
	int SizeY;

	        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 {
	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

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

	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


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?

Share this post

Link to post
Share on other sites
Since your cSpriteBase class lacks a default constructor you must supply an argument to its constructor when constructing a BoxType object.


BoxType() : cSpriteBase("box.file") {SizeX = 0; SizeY = 0;};

Share this post

Link to post
Share on other sites
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 called

Delete 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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!