Skip to main content
gamedev.net
🔒 Locked

Redefinition: different basic types (C++)

Started by CyberSlag5k Mar 31, 2007 at 11:15 AM 2 replies 7.8k views
Original Post
CyberSlag5k
CyberSlag5k
I'm writing code for a simple heightmap class and I'm just curious why one version of a method works but another doesn't. The following sections of code are just snippets, if it would help to see both definitions in full, they can be found here (for the next 24 hours): http://rafb.net/p/cx2IRf99.html Anyway, the situation at hand involves one constructor calling another. The first takes a string to a bitmap file, creates a bitmap object, and passes the bitmap to the second. Below are two different definitions for that first constructor. One works, the other does not. Can anyone tell me why?

class Terrain
{
public:
Terrain(const string& fileName);
Terrain(const Bitmap& bitmap);
};

// Works
Terrain::Terrain(const string& fileName)
{
	Terrain(Bitmap(fileName));
}

// Error: error C2371: 'bitmap' : redefinition; different basic types
Terrain::Terrain(const string& fileName)
{
	Bitmap bitmap(fileName);
	Terrain(bitmap);
}



What's even more peculiar is that I do almost the same thing in another section:

void Terrain::ProcessGeometry(const string& fileName)
{
	Bitmap bitmap(fileName);
	ProcessGeometry(bitmap);
}

void Terrain::ProcessGeometry(const Bitmap& bitmap)
{
	m_MapWidth = bitmap.GetWidth();
	m_MapLength = bitmap.GetHeight();

	InitializeMap();
	LoadHeightmap(bitmap);
	CalculateSurfaceNormals();
	CreateDisplayLists();
}



It seems to work with a method but not with a constructor. Maybe that's the distinction? EDIT: Also, is the working version more efficient, or is it too creating a temporary variable like the non-working version?
Without order nothing can exist - without chaos nothing can evolve.
deffer
deffer
Works? Say - compiles. It shouldn't work, since you can't call another constructor on the same object.

Terrain::Terrain(const string& fileName){  Terrain(Bitmap(fileName));}

is same as:

Terrain::Terrain(const string& fileName){  Terrain __a_temporary_object__(Bitmap(fileName));}


As for why you get an error there, I cannot see atm.

EDIT: this:
Terrain __a_temporary_object__(Bitmap(fileName));
seems to be a function declaration, change it to:
Terrain __a_temporary_object__( (Bitmap(fileName)) );
(if I'm not mistaken, again)[smile].
Enigma
Enigma
// WorksTerrain::Terrain(const string& fileName){	Terrain(Bitmap(fileName));}
This does not do what you think it does. It creates a new temporary object of type Terrain, which is then immediately destroyed. It does not call the Terrain(const Bitmap &) constructor for the current object.

If you want to delegate construction in this way you will need an initialisation function, i.e.:
class Terrain{	public:		Terrain(const string & fileName)		{			initalise(Bitmap(fileName));		}		Terrain(const Bitmap & bitmap)		{			initialise(bitmap);		}	private:		void initialise(const Bitmap & bitmap)		{			// what you were doing in the Terrain(const Bitmap &) function		}};

Σnigma
CyberSlag5k
CyberSlag5k
That makes sense. It's an easy fix, too, as I already have the functions in place to do properly.

Thanks for clearing that up!
Without order nothing can exist - without chaos nothing can evolve.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.