Jump to content
  • Advertisement
Sign in to follow this  
Antonym

no appropriate default constructor...

This topic is 2997 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

I have these three headers. PlayState and StartState inherit from GameState. When I try


#include "GameEngine.h"
#include "FreeImage.h"
#include <math.h>
#include "PlayState.h"
#include "StartState.h"

void GameEngine::start()
{
_gameState = new PlayState(); //SUCCEEDS
_gameState = new StartState(); //FAILS
}



I get
Error 2 error C2512: 'StartState' : no appropriate default constructor available c:\documents and settings\david\mis documentos\visual studio 2008\projects\blocks\blocks\gameengine.cpp 25 Blocks

I've been trying to find any mistake I've made but nothing. Any ideas as to what I could have missed?

Thanks.


#ifndef GAME_STATE_H
#define GAME_STATE_H

class GameEngine;

class GameState{
public:
virtual void init(GameEngine *gameEngine)=0;
virtual void process(GameEngine *gameEngine)=0;
virtual void update(GameEngine *gameEngine)=0;
virtual void render(GameEngine *gameEngine)=0;
virtual void cleanup(GameEngine *gameEngine)=0;
};

#endif




#ifndef START_STATE_H
#define START_STATE_H

#include "GameState.h"
#include "SFML\System.hpp"

class StartState : public GameState{
sf::Clock _controlTimer;
float _controlCooldown;
std::string _1Name;
std::string _2Name;
std::string _3Name;
std::string _1Score;
std::string _2Score;
std::string _3Score;

bool _editScore;
std::string &_edit;
int _char;

public:
void init(GameEngine *gameEngine);
void process(GameEngine *gameEngine);
void update(GameEngine *gameEngine);
void render(GameEngine *gameEngine);
void cleanup(GameEngine *gameEngine);
void save_scores();
};

#endif




#ifndef PLAY_STATE_H
#define PLAY_STATE_H

#include "GameState.h"
#include "Block.h"
#include "Cell.h"
#include "SFML\System.hpp"

class PlayState : public GameState{
bool _game_over;

block_g _playArea;

Block *_block;
Block *_next;

int _lines;
int _score;

int _consecutive;
bool _down;

sf::Clock _shiftTimer;
float _shiftCooldown;
sf::Clock _moveTimer;
float _moveCooldown;
sf::Clock _rotateTimer;
float _rotateCooldown;

bool _lock;
sf::Clock _lockTimer;
float _lockCooldown;
bool _fill;
sf::Clock _fillTimer;
float _fillCooldown;

int_l _complete;
bool _delete;
sf::Clock _blinkTimer;
float _blinkCooldown;
int _blinkIt;
bool _blink;

public:
void init(GameEngine *gameEngine);
void process(GameEngine *gameEngine);
void update(GameEngine *gameEngine);
void render(GameEngine *gameEngine);
void cleanup(GameEngine *gameEngine);

void draw_block(Block *block);
bool block(int x, int y);
void add(Block *b);
bool defeat();
void move(int x, int y);
void rotate();
bool bottom();
};

#endif



Share this post


Link to post
Share on other sites
Advertisement
In order for the compiler to automatically generate a default constructor for a class all of the member variables for that class must be default constructable. However, references are not default constructable. Either change the reference to a different type or add a constructor that initializes the reference.

Share this post


Link to post
Share on other sites
My general recommendation is to avoid using references as data members. It causes these sorts of problems and is also just logically awkward. The idea behind a reference is to give an alias to existing data - to let the reference be another name for the same data. The reference, in a sense, is what it refers to. So when you include that in a data structure, you're implying that data outside the object is a logical part of the object. When that actually is the case, the outside data should have the same lifetime as the object, which means you should be using some kind of smart pointer anyway. When it isn't (e.g. you're letting the caller decide what is referred-to, and it could be basically anything), you're just setting yourself up for a world of confusion IMO. Go with the smart pointer, again (although quite possibly a different kind of smart pointer).

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!