Public Group

# C++ Classes Help (Long Code Quote)

This topic is 4457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My code is shown below, as are the errors I am receiving. I cannot see what I am doing incorrectly here, however, whatever it is, I am most-likely doing it across all of my classes, thus causing the massive amount of errors seen. Class Location:
#pragma once

class Location
{
public:
Location::Location(int xco, int yco);
Location::Location(float xco, float yco);
Location::~Location(void);
int Location::GetX();
float Location::GetX();
int Location::GetY();
float Location::GetY();
void Location::SetX(int xco);
void Location::SeyX(float xco);
void Location::SetY(int yco);
void Location::SetY(float yco);
private:
float x;
float y;
};

Location::Location(int xco, int yco)
// Constructor using ints as positions
{
SetX(xco);
SetY(yco);
}

Location::Location(float xco, float yco)
// Constructor using floats as positions
{
SetX(int(xco));
SetY(int(yco));
}

Location::~Location(void)
{
}

int Location::GetX()
{
return (int(x));
}
float Location::GetX()
{
return x;
}
int Location::GetY()
{
return (int(y));
}
float Location::GetY()
{
return y;
}
void Location::SetX(int xco)
{
x = xco;
}
void Location::SetX(float xco)
{
x = xco;
}
void Location::SetY(float yco)
{
y = yco;
}
void Location::SetY(float yco)
{
y = yco;
}


Class Tile:
#pragma once

enum TileStatus
{
Sky;
BuildableIsland;
UnbuildableIsland;
UsedIsland;
Bridge;
};

enum TileSurroundings
{
EmptySurroundsAll;

};

class Tile
{
public:
Tile::Tile(Location TLoc);
Tile::Tile(int x, int y);
Tile::~Tile(void);
Location Tile::GetTileLocation();
void Tile::SetTileLocation(Location TLoc);
void Tile::SetTileLocation(int x, int y);
private:
TileStatus TheTileStatus;
Location TheTileLocation;
};

Tile(Location &TLoc)
{
TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile
TheTileLocation = TLoc; // sets the tile's location (x,y coordinates) to a predefined location on the map
}

Tile::Tile(int x, int y)
{
TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile
TheTileLocation = new Location(x,y); // sets the tile's location (x,y coordinates) to a predefined location on the map
}

Tile::~Tile(void)
{
}

Location Tile::GetTileLocation()
{
return TheTileLocation;
}
void Tile::SetTileLocation(Location &TLoc)
{
TheTileLocation = TLoc;
}
void Tile::SetTileLocation(int x, int y)
{
TheTileLocation = new Location(x, y);
}


Class Worldmap (my toplevel class that contains tiles and locations):
#include "Location.cpp"
#include "Tile.cpp"

#pragma once

class WorldMap
{
public:
WorldMap(void);
~WorldMap(void);
private:
ClearMap(Tile &TilesArray);
};

WorldMap::WorldMap(void)
{
// Called ONCE per game:
Tile TilesArray = Tile[256*32][256*32]; // There are (256 * 32) horizontal tiles per map, and (256 * 32) vertical tiles per map - thus making a map of 67,108,864 tiles.
ClearMap(&TilesArray);
}

WorldMap::~WorldMap(void)
{
ClearMap(&TilesArray);
}
void WorldMap::ClearMap(Tile &TilesArray)
{
for (int x = 0; x < (256 * 32); x++)
{
for (int y = 0; y < (256 * 32); y++)
{
TilesArray[x][y] = Tile(x, y);
}
}
}


The errors generated (multiples of the same errors, I realize):
Quote:
Now, can someone see what I am doing wrong, because I can't!

##### Share on other sites
enum TileStatus{	Sky,	BuildableIsland,	UnbuildableIsland,	UsedIsland,	Bridge};enum TileSurroundings{	EmptySurroundsAll};...Tile::Tile(Location &TLoc){	TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile	TheTileLocation = TLoc; // sets the tile's location (x,y coordinates) to a predefined location on the map}

They are elements. Use a ',' not a ';'. Also, you forgot the scope on the constructor. Thats all I noticed on a really quick glance.

Also, including cpp files is generally frowned upon. You should have header and code files. Including cpp files is going to lead to multiple declaration errors. You should split up definitions into header files, and declarations into source files.

##### Share on other sites
Okay, this will take a few minutes but the first error ive noticed is that you are declaring multiple functions that have the same signature, but return different values.

int Location::GetX();
float Location::GetX();

As both functions have the same name, when you call GetX() the compiler wont know which function to call. Hence, they have to be given different names, or removing one function entirely (you could probably remove the function that returns an int, as you could cast to an int from the returned float if need be)

A lot of those errors look like your missing semi colons, those kinds of errors should be found easy enough

##### Share on other sites
Are you not using header files? for instance a class.h and a class.cpp? Or are you writing it all in a .cpp file?

Also something that might help out a bit, at the beginning of each header file or what not add a,

#ifndef __LOCATION_H__
#define __LOCATION_H__

then at the end of the file add a,

#endif

that way they wont be included twice. Then at the top of yer tile class add a,

#include "Location.h"

or .cpp wherever its kept... that will allow yer tile class to use a Location object.

##### Share on other sites
I am using a .cpp and a .h file, and including CPPs and from there using a #include "classname.h" at the top of the file. I did not include this section and copy-pasted the code from the .Hs into the source
sections because I felt this post was long enough as it is. Sorry if I destroyed clarification in the process.

[Edited by - DragonGeo2 on April 4, 2006 3:53:10 PM]

##### Share on other sites
Quote:
 Original post by DragonGeo2I am using a .cpp and a .h file, and including CPPs and from there using a #include "classname.h" at the top of the file. I did not include this section and copy-pasted the code from the .Hs into the *** Source Snippet Removed *** sections because I felt this post was long enough as it is. Sorry if I destroyed clarification in the process.

ok I see... it would be nicer to see the headers and cpps seperate. Note that if its in the header file if you have,

class Location
{
Location::Location( void );
}

you only really need to do a,

class Location
{
Location( void );
}

EDIT:

also note that,

enum TileStatus
{
Sky;
BuildableIsland;
UnbuildableIsland;
UsedIsland;
Bridge;
};

needs to be:

enum TileStatus
{
Sky,
BuildableIsland,
UnbuildableIsland,
UsedIsland,
Bridge
};

Because yer really making them 0, 1, 2, 3, 4,... which is one line of code, not multiple as u used ";"

##### Share on other sites
Also note:
Tile::Tile(int x, int y){	TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile	TheTileLocation = new Location(x,y); // sets the tile's location (x,y coordinates) to a predefined location on the map}

is fine, but in yer header you need to change the definition of TheTileLocation to :

Location* TheTileLocation;

its really a pointer to the heap when u use 'new'. So then in yer ~Tile() put a:

Tile::~Tile(void){     if(TheTileLocation != NULL)delete TheTileLocation;}

EDIT:

Also note that Tile does not have a default constructor: Tile( void );

so if you go to create an array like you have tile[..][..], you need to pass in a variable either a Tile(int, int), or a Tile(Location).

##### Share on other sites
Quote:
 Also note that Tile does not have a default constructor: Tile( void );so if you go to create an array like you have tile[..][..], you need to pass in a variable either a Tile(int, int), or a Tile(Location).

I did this on purpose because I wanted the Tile object to automatically assume its location is 0,0 and that it be actually told its location on the worldmap that way - it seems to make it easier to code, as you can never have a tile that doesn't know its own location (because that would be purposeless).

##### Share on other sites
Quote:
Original post by DragonGeo2
Quote:
 Also note that Tile does not have a default constructor: Tile( void );so if you go to create an array like you have tile[..][..], you need to pass in a variable either a Tile(int, int), or a Tile(Location).

I did this on purpose because I wanted the Tile object to automatically assume its location is 0,0 and that it be actually told its location on the worldmap that way - it seems to make it easier to code, as you can never have a tile that doesn't know its own location (because that would be purposeless).

Ok then under yer class Tile, make a,

class Tile{...     Tile(void);...}Tile::Tile( void ){	TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile	TheTileLocation = new Location(0,0); // sets the tile's location (0,0 coordinates) to a predefined location on the map}

you see, you didnt have a default constructor that was being called, you were calling Tile( void ); but it didnt exist... so it wasnt setting anything to 0 like you wanted. IE, it was calling a function that didnt exist.

##### Share on other sites
After about ten or so revisions based on the feedback of the community, I was able to reduce the number of errors (and unfortunately increase the number of warnings) by a factor of 0.5. I've added more default constructors with (void) arguments, fixed the problems with using semicolons instead of commas in the enumerations, and even increased the scope in that pesky WorldMap constructor. This is good progress, however, the code still won't compile. I'm going to post the revised files' sources up again, but this time, in a manner that is easier to understand for most.
Quote:

Now, the individual class' files (with .H files being called first, and listed first):

WorldMap.h:
#ifndef Worldmap_H#define Worldmap_H#pragma once#include "Tile.h";class WorldMap{public:	WorldMap(void);	~WorldMap(void);private:	ClearMap(void);	Tile TilesArray[256*32][256*32]; // Updated the scope of this to exist outside of the constructor};#include "WorldMap.cpp";#endif

Now, WorldMap.cpp:
#ifndef Worldmap_CPP#define Worldmap_CPPWorldMap(void){	// Called ONCE per game:	// There are (256 * 32) horizontal tiles per map, and (256 * 32) vertical tiles per map - thus making a map of 67,108,864 tiles.	ClearMap(&TilesArray);}~WorldMap(void){	ClearMap();}void WorldMap::ClearMap(void){	for (int x = 0; x < (256 * 32); x++)	{		for (int y = 0; y < (256 * 32); y++)		{			TilesArray[x][y] = Tile(x, y);		}	}}#endif

Now for Tile.h:
#ifndef Tile_H#define Tile_H#pragma once#include "TileStatus.h";#include "Location.h";class Tile{public:	Tile(void);	Tile(Location TLoc);	Tile(int x, int y);	~Tile(void);	Location GetTileLocation();	void SetTileLocation(Location TLoc);	void SetTileLocation(int x, int y);private:	TileStatus TheTileStatus;	Location TheTileLocation;};#include "Tile.cpp";#endif

And the enumerations file, TileStatus.h:
enum TileStatus{	Sky,	BuildableIsland,	UnbuildableIsland,	UsedIsland,	Bridge};enum TileSurroundings{	EmptySurroundsAll};

And Tile.cpp:
#ifndef Tile_CPP#define Tile_CPP#ifndef Location_H#include "Location.h";#endifTile(void){	TheTileStatus = Sky;	TheTileLocation = new Location(0,0);}Tile(Location &TLoc){	TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile	TheTileLocation = TLoc; // sets the tile's location (x,y coordinates) to a predefined location on the map}Tile(int x, int y){	TheTileStatus = Sky; // initializes the status of the tile to be "sky" or blank tile	Location* TheTileLocation = new Location(x,y); // sets the tile's location (x,y coordinates) to a predefined location on the map}~Tile(void){	if(TheTileLocation != NULL)	{		delete TheTileLocation;	}}Location Tile::GetTileLocation(){	return TheTileLocation;}void Tile::SetTileLocation(Location &TLoc){	TheTileLocation = TLoc;}void Tile::SetTileLocation(int x, int y){	TheTileLocation = new Location(x, y);}#endif

Finally, Location.h:
#ifndef Location_H#define Location_H#pragma onceclass Location{public:	Location(void);	Location(int xco, int yco);	Location(float xco, float yco);	~Location(void);	float GetX();	float GetY();	void SetX(int xco);	void SeyX(float xco);	void SetY(int yco);	void SetY(float yco);private:	float x;	float y;};#include "Location.cpp";#endif

And last but not least, Location.cpp:
#ifndef Location_CPP#define Location_CPPLocation(void){	SetX(0);	SetY(0);}Location(int xco, int yco)// Constructor using ints as positions{	SetX(xco);	SetY(yco);}Location(float xco, float yco)// Constructor using floats as positions{	SetX(int(xco));	SetY(int(yco));}~Location(void){}float Location::GetX(){	return x;}float Location::GetY(){	return y;}void Location::SetX(int xco){	x = (float(xco));}void Location::SetX(float xco){	x = xco;}void Location::SetY(int yco){	y = (float(yco));}void Location::SetY(float yco){	y = yco;}#endif

Thanks for all the help people have reccomended previously, let's keep up the good work people!

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631365
• Total Posts
2999581
×