constructor problem

Started by
2 comments, last by aarbron 16 years, 3 months ago
I get the error " 'State' : no appropriate default constructor available " I'm trying to pass a function pointer in the constructor, is my syntax wrong ?

//==== state.h

class State
	{
	protected:
	void (*changeCallback)(State*);//owner's 'changestate' func
	public:
		State(void (*x)(State*)):changeCallback(x){};
		virtual ~State(){};
		virtual int Logic()=0;
		virtual void Render()=0;
	};

//==== State_GameMenu.h
#pragma once
#include "state.h"
#include "renderer.h"

class State_GameMenu : public State
	{
	public:
		State_GameMenu(void (*)(State*));
		~State_GameMenu(void);
		virtual int Logic();
		virtual void Render();
	};

//==== State_GameMenu.cpp
#include "State_GameMenu.h"

State_GameMenu::State_GameMenu(void (*cb)(State*))
	{
//error points here
	}

State_GameMenu::~State_GameMenu(void)
	{
	}
//etc.

Advertisement
You need to pass an argument to the State constructor. Ex:
State_GameMenu::State_GameMenu(void (*cb)(State*)) : State(cb)	{	}
Unless you specify otherwise, the constructor of a derived class will call the default constructor of the base class. Since you haven't defined one, you get an error. If you want it to call a parameterized constructor instead, do this:

State_GameMenu::State_GameMenu(void (*cb)(State*)) : State (cb)


Also if you're going to be using callbacks, I recommend considering boost::function.
Great, thanks for the help and clarification.

This topic is closed to new replies.

Advertisement