I have the following two classes.
Code:
class Surface;
class Sprite;
Surface is just a basic wrapper for some of the SDL functions, and holds clipping information, which is unimportant at the moment.
Sprite contains a reference to Surface.
Code:
class Sprite
{
private:
Surface& sprite;
//other members here
public:
//functions and stuff here
};
Naturally it will not allow me to compile this because it has to initialize sprite(the Surface&).
Code:
1>------ Build started: Project: Project-Varia, Configuration: Debug Win32 ------
1>Compiling...
1>Sprite.cpp
1>c:\documents and settings\raigne\my documents\visual studio 2008\projects\project-varia\project-varia\sprite.cpp(6) : error C2440: 'initializing' : cannot convert from 'int' to 'Surface &'
1>c:\documents and settings\raigne\my documents\visual studio 2008\projects\project-varia\project-varia\sprite.cpp(6) : error C2439: 'Sprite::sprite' : member could not be initialized
1> c:\documents and settings\raigne\my documents\visual studio 2008\projects\project-varia\project-varia\sprite.h(6) : see declaration of 'Sprite::sprite'
1>Build log was saved at "file://c:\Documents and Settings\Raigne\My Documents\Visual Studio 2008\Projects\Project-Varia\Project-Varia\Debug\BuildLog.htm"
1>Project-Varia - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It says that it must be done in the initializer list, but I cant figure it out. The full Sprite, and Surface classes are below. I greatly appreciate any insight as to the solution to my predicament.
Code:
#pragma once
class Surface
{
private:
SDL_Surface* surface;
std::vector<SDL_Rect> clips;
bool usable_;
bool debug_;
public:
//Constructor
Surface();
Surface(const std::string& imagefile);
//Destructor
~Surface();
//Debug
void SetDebug(bool d) { debug_ = d; };
//Surface usability function
const bool& GetUsable() const { return usable_; };
//Compare rects function
bool ComRects(const SDL_Rect& a, const SDL_Rect& b);
//Access the surface
SDL_Surface* GetSurface();
//Access rect by ID
SDL_Rect& GetClip(const unsigned int& rectID) { if ( rectID <= clips.size() ) { return clips.at(rectID); } };
//Load a new surface
bool LoadSurface(const std::string& imagefile);
//Free surface
void FreeSurface() { SDL_FreeSurface(surface); };
//Create a new clip
void CreateClip(const int& x, const int& y, const int& w, const int& h);
//Delete a clip
void DeleteClip(const int& clipID);
};
Code:
#pragma once
class Sprite
{
private:
Surface& sprite;
SDL_Rect position;
std::vector<std::vector<int> > clipbook;
struct
{
short cur_page;
short cur_clip;
short start_clip;
short last_clip;
};
public:
Sprite();
~Sprite();
void SetSprite(Surface& surface);
Surface& GetSprite() { return sprite; };
//clipbook functions
void AddPage(); //adds a page to the back of the book
void AddClip(const int& page, const int& clip);//adds a clip to the end of a page
void WarpSprite(const int& x, const int& y);
void MoveSprite(const int& x_axis, const int& y_axis);
void AnimateClipPage(const int& page, const int& start_clip = 0);
};