Jump to content
  • Advertisement
Sign in to follow this  
Mybowlcut

Error C2514 - Class has no constructors

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

This has gotta be one of the most $%^$% errors ever:
Quote:
Error 1 error C2514: 'Tile' : class has no constructors c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 47
Can anyone tell me why it's happening? The code is C++ obviously and I'm using VS2005 version 8 with the standard compiler (whatever that is). Here's my Level ctor:
Level::Level(std::string n,
			 ushort size_w,
			 ushort size_h,
			 ushort cam_x,
			 ushort cam_y,
			 ushort cam_w,
			 ushort cam_h)
			 :
			 name(n)
{
	size.w = size_w;
	size.h = size_h;
	camera.x = cam_x;
	camera.y = cam_y;
	camera.w = cam_w;
	camera.h = cam_h;

	/* Initialise tiles. */
	tiles.resize(size_h);

	SDL_Rect clip;
	ushort tile_no = 0;
	std::string name = "";

	for(ushort i = 0; i < size_h; ++i)
	{
		for(ushort j = 0; i < size_w; ++j, ++tile_no)
		{
			clip.x = j * TILE_WIDTH + TILE_BORDER;
			clip.y = i * TILE_HEIGHT + TILE_BORDER;
			clip.w = TILE_WIDTH;
			clip.h = TILE_HEIGHT;
			
			if(tile_no >= 10)
				name = "T_0" + ToString(tile_no);
			else
				name = "T_00" + ToString(tile_no);

			tiles.push_back(Tile(name,
								 "",
								 INVISIBLE,
								 AC_NOTHING,
								 j * TILE_WIDTH,
								 i * TILE_HEIGHT,
								 clip));// Error is here		
		}
	}
}




Level.h file:
// Checkers
// Created by Mitch Curtis, 2007

#ifndef IMAGE_H
#define IMAGE_H

#include <string>
#include <vector>
#include <iostream>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "Misc.h"
#include "Draw_Engine.h"
#include "Timer.h"
#include "Image.h"
#include "User_Interface.h"

typedef unsigned short int ushort;

class Tile;

class Level
{
public:
	Level(std::string n,
		  ushort size_w,
		  ushort size_h,
		  ushort cam_x,
		  ushort cam_y,
		  ushort cam_w,
		  ushort cam_h);
	twodindex Get_Tile_Hit(SDL_Rect mouse);
	void Clear_Tiles();
	void Apply_Tile_Pen();
	void Set_Tile_At(std::string tile, std::string to_this);
	void Set_Camera(ushort x, ushort y, ushort w, ushort h);
	void Nudge_Camera(DIRECTION dir);
protected:
	std::string name;
	std::vector<std::vector<Tile> > tiles;
	SDL_Rect size;
	SDL_Rect camera; // colour key
};

#endif




Tile class:
class Tile : public Image
{
public:
	Tile(std::string n,
		 std::string status_msg,
		 PRIORITY p,
		 ACTION_CODE ac,
		 int x,
		 int y,
		 SDL_Rect c,
		 SDL_Surface* s = NULL);
	std::string Get_Name() const;
	void Set_Name(std::string new_name);
	ACTION_CODE Get_Action_Code() const;
	std::string Get_Status_Bar_Msg() const;
private:
	std::string name;
	ACTION_CODE action_code;
	std::string status_bar_msg;
};





Tile ctor:
Tile::Tile(std::string n,
		   std::string status_msg,
		   PRIORITY p,
		   ACTION_CODE ac,
		   int x,
		   int y,
		   SDL_Rect c,
		   SDL_Surface* s)
		   :
		   name(n),
		   status_bar_msg(status_msg),
		   action_code(ac)
{
	surface = s;
	ToClip(clip, c);
	info.x = x;
	info.y = y;
	info.w = TILE_WIDTH;
	info.h = TILE_HEIGHT;
	priority = p;
	is_colour_key = true;
	is_clip = true;
}




I'm posting so much code just in case someone asks for it.. and 'cause I'm el stupido when it comes to forward declarations and that $#^#. Edit: Oh yeah, there's also THESE:
Quote:
Error 2 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 61 Error 3 error C2228: left of '.Get_Info' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 61 Error 4 error C2228: left of '.x' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 61 Error 5 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 61 Error 6 error C2228: left of '.Get_Info' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 61 Error 7 error C2228: left of '.x' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 61 Error 8 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 62 Error 9 error C2228: left of '.Get_Info' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 62 Error 10 error C2228: left of '.y' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 62 Error 11 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 62 Error 12 error C2228: left of '.Get_Info' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 62 Error 13 error C2228: left of '.y' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 62 Error 14 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 78 Error 15 error C2228: left of '.Set_Name' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 78 Error 16 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 89 Error 17 error C2228: left of '.Get_Name' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 89 Error 18 error C2027: use of undefined type 'Tile' c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 90 Error 19 error C2228: left of '.Set_Name' must have class/struct/union c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\level.cpp 90
Which I'm guessing are a result of the first error. Sorry about all the code once again. :)

Share this post


Link to post
Share on other sites
Advertisement
1. include Tile.h in Level.cpp

2. since Tile inherite from Image so don't forget to construct Image if you don't have default constructor in Image class ( Image() )

Share this post


Link to post
Share on other sites
Quote:
Original post by Neon2302
1. include Tile.h in Level.cpp

2. since Tile inherite from Image so don't forget to construct Image if you don't have default constructor in Image class ( Image() )



Ooo that's gonna be a problem. For "simplicity" reasons I've kept all classes derived from image in Image.h:

// Checkers
// Created by Mitch Curtis, 2007

#ifndef IMAGE_H
#define IMAGE_H

#include <string>
#include <vector>
#include <iostream>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "Misc.h"
#include "Draw_Engine.h"
#include "Timer.h"

typedef unsigned short int ushort;

class Draw_Engine;

class Image
{
public:
Image();
Image(bool clip,
bool colour_key,
std::string f_name,
PRIORITY p = IMAGE,
int x = 0,
int y = 0);

void Set_Surface(SDL_Surface* optimized);
SDL_Surface* Get_Surface();
SDL_Rect Get_Info() const;
std::string Get_File_Name() const;
int Get_X() const;
int Get_Y() const;
int Get_W() const;
int Get_H() const;
void Set_X(int x);
void Set_Y(int y);
void Set_W(int w);
void Set_H(int h);
void Set_Position(int x, int y);
void Set_Info(const SDL_Rect& new_info);
void Set_Info(bool clip, bool colour_key, std::string f_name, int x, int y);
PRIORITY Get_Priority() const;
void Set_Priority(PRIORITY p);
bool Is_Clip() const;
SDL_Rect* Get_Clip();
void Set_Clip(const SDL_Rect& new_clip);
void Set_Clip(int x, int y, int w, int h);
bool Is_Colour_Key() const;
Uint8 Get_Red() const;
Uint8 Get_Blue() const;
Uint8 Get_Green() const;
void Set_RGB_Key(std::vector<Uint8> RGB);
void Set_RGB_Key(Uint8 r = 0xFF, Uint8 g = 0xFF, Uint8 b = 0xFF);
bool Is_Mouse_Over(const SDL_Rect& mouse) const;
static bool Is_Mouse_Over(const SDL_Rect& mouse, const SDL_Rect& area);
bool Is_Clip_Equal_To(const SDL_Rect& a_clip) const;
RETURN_CODE Handle_Drag(Draw_Engine& DE, SDL_Event& event_);
RETURN_CODE Handle_Slide(Draw_Engine& DE,
SDL_Event& event_,
ushort left_bound,
ushort right_bound,
AXIS axis);
protected:
std::string file_name;
SDL_Surface* surface;
SDL_Rect info;
bool is_clip;
SDL_Rect clip;
bool is_colour_key;
std::vector<Uint8> RGB_key; // colour key
PRIORITY priority;
};

class Button : public Image
{
public:
Button(ACTION_CODE ac,
std::string fn,
std::string status_msg,
bool colour_key,
PRIORITY p,
int x,
int y,
SDL_Rect off,
SDL_Rect over,
SDL_Rect down);

ACTION_CODE Get_Action() const;
void Set_Action(ACTION_CODE ac) ;
std::string Get_Status_Bar_Msg() const;
void Set_Status_Bar_Msg(std::string msg);
CLIP_STATE Get_Current_State() const;
void Set_Current_State(CLIP_STATE state);
void Set_States(SDL_Rect off, SDL_Rect over, SDL_Rect down);
private:
ACTION_CODE action;
std::string status_bar_msg;
CLIP_STATE current_state;
SDL_Rect off_clip;
SDL_Rect over_clip;
SDL_Rect down_clip;
};

class Charset : public Image
{
public:
Charset(std::string fn,
std::string status_msg,
bool colour_key,
PRIORITY p,
int x,
int y,
Uint32 s);

Uint32 Get_Animation_Speed() const;
CLIP_STATE Get_State() const;
void Set_State(CLIP_STATE new_state);
CLIP_STATE Get_Previous_State() const;
void Set_Previous_State(CLIP_STATE new_state);
bool Is_Animating() const;
void Start_Animation();
void Stop_Animation();
bool Update_State();
private:
Timer timer;
Uint32 animation_speed;
CLIP_STATE state;
CLIP_STATE previous_state;
std::string status_bar_msg;
bool animating;
};

class Tile : public Image
{
public:
Tile(std::string n,
std::string status_msg,
PRIORITY p,
ACTION_CODE ac,
int x,
int y,
SDL_Rect c,
SDL_Surface* s = NULL);
std::string Get_Name() const;
void Set_Name(std::string new_name);
ACTION_CODE Get_Action_Code() const;
std::string Get_Status_Bar_Msg() const;
private:
std::string name;
ACTION_CODE action_code;
std::string status_bar_msg;
};

#endif




:o

Also, why would I include a header into a .cpp? I've never done that unless it's the header of the class that's being defined, or it's the .cpp that main is in.

Share this post


Link to post
Share on other sites
Header files should be included in .cpp files instead of inside other header files when at all possible. Use things like forward declaration so that you don't need the include in the header file.

For starters, it'll speed up compile times.
It'll also help prevent circular include problems, make for better encapsulation, less possibility of name clashes... the list goes on...

Share this post


Link to post
Share on other sites
keep everything in .h file will face you a problem (I had occur once :P )

since sometime you need to refer from class A to class B and class B to class A

so that if you include A.h in B B can see a but A can't see B and other way round if you include B.h in A

so at last separate coding and declaration (I mean .h and .cpp) is the only one solution. (As I know)

I'm the one who want to coding as your style (closely to Java). But it can't in C

Share this post


Link to post
Share on other sites
Ok, I'm in the process of doing it now, but it involves a lot of work. I'd just like to say that if there's anything UGLY and DISGUSTING about C++ it's this forward declaration stuff... it LOOKS horrible and is a real pain in the arse. :@

Share this post


Link to post
Share on other sites
Ok, done. I put the derived classes (Button, Charset and Tile) into separate .h files. Now I'm getting more errors:

Quote:
Error 1 error C2504: 'Image' : base class undefined c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\tile.h 12
Error 2 error C2504: 'Image' : base class undefined c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\button.h 12
Error 3 error C2504: 'Image' : base class undefined c:\documents and settings\user\my documents\visual studio 2005\projects\hex\earth\earth\charset.h 12


Button:
#ifndef BUTTON_H
#define BUTTON_H

#include <string>

#include "Image.h"
#include "Misc.h"

class Image;

class Button : public Image
{
public:
Button(ACTION_CODE ac,
std::string fn,
std::string status_msg,
bool colour_key,
PRIORITY p,
int x,
int y,
SDL_Rect off,
SDL_Rect over,
SDL_Rect down);

ACTION_CODE Get_Action() const;
void Set_Action(ACTION_CODE ac) ;
std::string Get_Status_Bar_Msg() const;
void Set_Status_Bar_Msg(std::string msg);
CLIP_STATE Get_Current_State() const;
void Set_Current_State(CLIP_STATE state);
void Set_States(SDL_Rect off, SDL_Rect over, SDL_Rect down);
private:
ACTION_CODE action;
std::string status_bar_msg;
CLIP_STATE current_state;
SDL_Rect off_clip;
SDL_Rect over_clip;
SDL_Rect down_clip;
};

#endif

Charset:
#ifndef CHARSET_H
#define CHARSET_H

#include <string>

#include "Image.h"
#include "Misc.h"

class Image;

class Charset : public Image
{
public:
Charset(std::string fn,
std::string status_msg,
bool colour_key,
PRIORITY p,
int x,
int y,
Uint32 s);

Uint32 Get_Animation_Speed() const;
CLIP_STATE Get_State() const;
void Set_State(CLIP_STATE new_state);
CLIP_STATE Get_Previous_State() const;
void Set_Previous_State(CLIP_STATE new_state);
bool Is_Animating() const;
void Start_Animation();
void Stop_Animation();
bool Update_State();
private:
Timer timer;
Uint32 animation_speed;
CLIP_STATE state;
CLIP_STATE previous_state;
std::string status_bar_msg;
bool animating;
};

#endif

Tile:
#ifndef TILE_H
#define TILE_H

#include <string>

#include "Image.h"
#include "Misc.h"

class Image;

class Tile : public Image
{
public:
Tile(std::string n,
std::string status_msg,
PRIORITY p,
ACTION_CODE ac,
int x,
int y,
SDL_Rect c,
SDL_Surface* s = NULL);
std::string Get_Name() const;
void Set_Name(std::string new_name);
ACTION_CODE Get_Action_Code() const;
std::string Get_Status_Bar_Msg() const;
private:
std::string name;
ACTION_CODE action_code;
std::string status_bar_msg;
};

#endif

Share this post


Link to post
Share on other sites
Quote:
Original post by Mybowlcut
I'd just like to say that if there's anything UGLY and DISGUSTING about C++ it's this forward declaration stuff... it LOOKS horrible and is a real pain in the arse. :@

If you've come from Java to C++, yes it's ugly and a pain; if you've come from C to C++, no, it makes perfect sense and looks clean :)

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Header files should be included in .cpp files instead of inside other header files when at all possible. Use things like forward declaration so that you don't need the include in the header file.

For starters, it'll speed up compile times.
It'll also help prevent circular include problems, make for better encapsulation, less possibility of name clashes... the list goes on...


Could you please clarify what you meant by this? Are you saying that instead of including my Button, Charset and Tile .h files wherever I need them, that I can just use a forward declaration instead? Or do you mean that I can use a forward declaration in my Button, Charset and Tile .h files instead of including the Image.h file (base class)?

Cheers.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!