Jump to content
  • Advertisement
Sign in to follow this  
Jemburula

Constructors

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

Hey just having some trouble with my constructors. I wanted my constructor to originally only be TileEngine::TileEngine(int ScreenSizeX, int ScreenSizeY) but VC++.NET spat this at me "error C2512: 'TileEngine' : no appropriate default constructor available" so I guessed I needed an TileEngine::TileEngine() so I added it and I want to just get it to pass off some default values to the second constructor. I'm not sure if this is possible or what the syntax was but I took a stab down in my code there.
TileEngine::TileEngine() : TileEngine::TileEngine(640, 480)
{
  return;
};

TileEngine::TileEngine(int ScreenSizeX, int ScreenSizeY)
{
  Sdl = Sdl->GetInstance();

  Tile TileTemp;

  TileTemp.Image = Sdl->LoadBmp("Grass");
  TileTemp.Block = true;
  this->TileData["Grass"] = TileTemp;

  this->Resize(ScreenSizeX, ScreenSizeY);
  return;
};

Also when I create the object I do it like so: TileEngine* TileEngine(640, 480) = TileEngine->GetInstance(); but this causes errors :( so how do I properly pass values to my constructor like that? Thanks for your help everyone :) -Jemburula

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Jemburula
Hey just having some trouble with my constructors. I wanted my constructor to originally only be TileEngine::TileEngine(int ScreenSizeX, int ScreenSizeY) but VC++.NET spat this at me "error C2512: 'TileEngine' : no appropriate default constructor available" so I guessed I needed an TileEngine::TileEngine() so I added it and I want to just get it to pass off some default values to the second constructor. I'm not sure if this is possible or what the syntax was but I took a stab down in my code there.


To make a default constructor out of one which is non default give all parameters a default value:


struct TileEngine {

TileEngine(int ScreenSizeX = 640, int ScreenSizeY = 480);
//....
};


Quote:
Original post by Jemburula
Also when I create the object I do it like so:
TileEngine* TileEngine(640, 480) = TileEngine->GetInstance();
but this causes errors :( so how do I properly pass values to my constructor like that?


There are a thew ways you can go about this, the simplest method would be just to have those as function parameters in TileEngine::GetInstance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jemburula
Hey just having some trouble with my constructors. I wanted my constructor to originally only be TileEngine::TileEngine(int ScreenSizeX, int ScreenSizeY) but VC++.NET spat this at me "error C2512: 'TileEngine' : no appropriate default constructor available" so I guessed I needed an TileEngine::TileEngine() so I added it and I want to just get it to pass off some default values to the second constructor. I'm not sure if this is possible or what the syntax was but I took a stab down in my code there.


You can't call another constructor from a constructor. Initializer lists can only be used to pass arguments to the constructors of the base classes and the members, not the class itself.

The common way to this is to create a non-constructor initialization function that gets called from both constructors.

Quote:

Also when I create the object I do it like so:
TileEngine* TileEngine(640, 480) = TileEngine->GetInstance();
but this causes errors :( so how do I properly pass values to my constructor like that?


If you want to create a pointer, and make it point to the current instance of
the tile engine (which is returned by the GetInstance static function), you would write:

TileEngine * tileEngine = TileEngine::GetInstance( );

Share this post


Link to post
Share on other sites
I'm not learning SDL or anything your doing yet, but I've done constructors for awhile. If you need a Defualt constructor that you can pass parameters to and just have default ones in it. Just do this: (using an example)


TileEngine::TileEngine(int ScreenSizeX = 640, int ScreenSizeY = 480)
{
Sdl = Sdl->GetInstance();

Tile TileTemp;

TileTemp.Image = Sdl->LoadBmp("Grass");
TileTemp.Block = true;
this->TileData["Grass"] = TileTemp;

this->Resize(ScreenSizeX, ScreenSizeY);
return;
};



So this means that you can either do this:

TileEngine myEngine;

which makes an instance of the TileEngine which the screen size of X = 640 and Y = 480 at default, or you can do this:

TileEngine myEngine(some x value, some y value);

which will set the screen X and Y values to the parameters passed in.

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!