# C++ Classes Help (Long Code Quote)

This topic is 4342 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!

##### Share on other sites
I thought the other posters were pretty clear on this point, but then again I'm not a newbie, so I forgive you :)

DO NOT use a line like #include <something.cpp>

You only include header files in cpp files and header files. You never include cpp files.

Basically, you put class/struct definitions, function prototypes, global variables(as extern), typedefs, constants, and #defines

in order, this is the stuff above.
#ifndef HEADER_H#define HEADER_H#include <string> /* we use string, so include it *//* class definition */class CClass { /* or struct in place of class, they're identical in C++ */ private:  int private_var;public:  CClass();  ~CClass();  int getPrivate();};/* function prototype */void printSomething(const std::string &something);/* global variable */extern int I_AM_EVERY_WHERE;/* typedefinition */typedef CClass aClass;/* constant */const int bad_boy = 10;/* define */#define square(x) x*x#endif

and the associated cpp file header.cpp
#include <iostream> #include <string> /* we use iostream, string and header.h, so include them */#include "header.h"CClass::CClass { private_var = 0; }CClass::~CClass { /* unitialize stuff */ }int CClass::getPrivate() { return private_var; }int I_AM_EVERY_WHERE;/* note how I'm prefixing string, cout, and endl with std::, because * you should avoid using using namespace std; */void printSomething(const std::string &something) {  std::cout << something << std::endl;}

And finally, in your main.cpp file you'd do something like this:
#include <iostream>#include <header.h>int main(){  aClass temp;  std::cout << "private: " << temp.getPrivate(); << std::endl;  return 0;}

There are times when you are including a cpp file in your header file. I'm sure this is causing several problems as the very first error looks like it's coming from tile.h in line 12. there's nothing wrong with this line which means there error comes from earlier. so I looked in last header file you added and looked in location.h

At the bottom of location.h you include location.cpp. So logically I'm going to assume the error is there, but because you shouldn't be including cpp files in header files(or anywhere for that matter) I'm going to tell you to read that link and change your code until you don't include cpp files and then if you're still having problems I'll help.

##### Share on other sites
You mispelled SetX in your location class. You have SetX() and SeyX().

Edit: You also have semi-colons after some of your include directives (#include).

##### Share on other sites
Quote:
 You mispelled SetX in your location class. You have SetX() and SeyX().

Thanks but I'm still getting a zillion errors.

##### Share on other sites
Problem one,
------------------------------------------------------------------------------
Under...

Class Tile
{
...
}

yer,
Location TheTileLocation;
needs to be changed to,
Location* TheTileLocation;

Then in make yer,

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

Next problem:
------------------------------------------------------------------------------
What the last poster said, goto the top of WorldMap.h and replace:

#include "Location.cpp"
#include "Tile.cpp"

with,

#include "Location.h"
#include "Tile.h"

Also, like the last poster said, YOU SHOULD NEVER INCLUDE .CPP.
that being said, at the end of Tile.h take out the include .cpps...they are not needed.

Next problem:
--------------------------------------------------------------------------------
you dont need to if define any CPP files, just the at the top of yer .h files. So you can safely remove those

Next problem:
-----------------------------------------------------------------------------

Under yer location class, you cant have two functions of the same name return 2 different variables...

So, all the GetX() and GetY() functions need to have different names, like so:

Location::GetXAsFloat(), GetYAsInt()...
The Set..() functions are fine because the input is another type... its just the output needs different.

##### Share on other sites
Alright, after much cleaning-up and revising the code as many times as needed to conform to the standards of the posters, I'm still getting some of the errors I've been getting this entire time. Most notably:
Quote:
 h:\NSRM\Location.cpp(6): error C2065: 'SetX' : undeclared identifierh:\NSRM\Location.cpp(7): error C2065: 'SetY' : undeclared identifierh:\NSRM\Location.cpp(8): warning C4508: 'Location' : function should return a value; 'void' return type assumedh:\NSRM\Location.cpp(14): warning C4508: 'Location' : function should return a value; 'void' return type assumedh:\NSRM\Location.cpp(21): warning C4508: 'Location' : function should return a value; 'void' return type assumedh:\NSRM\Location.cpp(23): error C2588: '::~Location' : illegal global destructorh:\NSRM\Location.cpp(24): error C2084: function 'int Location(void)' already has a bodyh:\NSRM\Location.cpp(27): error C2653: 'Location' : is not a class or namespace nameh:\NSRM\Location.cpp(29): error C2065: 'x' : undeclared identifierh:\NSRM\Location.cpp(31): error C2653: 'Location' : is not a class or namespace nameh:\NSRM\Location.cpp(33): error C2065: 'y' : undeclared identifierh:\NSRM\Location.cpp(35): error C2653: 'Location' : is not a class or namespace nameh:\NSRM\Location.cpp(36): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'h:\NSRM\Location.cpp(39): error C2653: 'Location' : is not a class or namespace nameh:\NSRM\Location.cpp(40): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'h:\NSRM\Location.cpp(43): error C2653: 'Location' : is not a class or namespace nameh:\NSRM\Location.cpp(44): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'h:\NSRM\Location.cpp(47): error C2653: 'Location' : is not a class or namespace nameh:\NSRM\Location.cpp(48): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'h:\NSRM\Tile.cpp(6): error C2065: 'TheTileStatus' : undeclared identifierh:\NSRM\Tile.cpp(6): error C2065: 'Sky' : undeclared identifier

When double-clicking on these items in the IDE's error window - it takes me to the line shown. There seems to be confusion within the progrm as to the definition of the Location class, although I made sure to put #include "Location.h" before Tile and WorldMap in the main file.
Does anyone know why this is happening?
-DragonGeo2

##### Share on other sites
http://img360.imageshack.us/img360/4253/undefined4jf.png
I don't see why it thinks "Location" is an "undefined identifier" although the IDE simply finds it quite defined, as per this screenshot...
-DG

##### Share on other sites
Don't write this much code at once until you have practice figuring out these things on your own. Take out a bunch of stuff, get the remaining things to *work*, and then put the rest back in gradually.

##### Share on other sites
Yeah it kinda works when I comment out the line:
#include "WorldMap.h"

Can anyone answer me why my program isn't working?

##### Share on other sites
Quote:
 Original post by DragonGeo2Yeah it kinda works when I comment out the line:#include "WorldMap.h"Can anyone answer me why my program isn't working?

I would agree with the other poster, you really should pickup a good book on programming basics.... All these errors would be understandable even by reading Visual C++ in 21 Days. Published by Sams publishing(I think still).

here are some of yer errors, lets see if we can understand them:

h:\NSRM\Location.cpp(6): error C2065: 'SetX' : undeclared identifier
h:\NSRM\Location.cpp(7): error C2065: 'SetY' : undeclared identifier

-These 2 mean that SetX() and SetY() dont exist, Probably because u renamed them? double click the error to find where it is calling the wrong name.

h:\NSRM\Location.cpp(8): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(14): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(21): warning C4508: 'Location' : function should return a value; 'void' return type assumed

-These warnings simply mean you did not return anything from Location. Try returning void...

h:\NSRM\Location.cpp(23): error C2588: '::~Location' : illegal global destructor
h:\NSRM\Location.cpp(24): error C2084: function 'int Location(void)' already has a body
-You have two bodies of code for Location, try finding where the second is and take it out. remove it most likely.

h:\NSRM\Location.cpp(27): error C2653: 'Location' : is not a class or namespace name
-This just means that because of the previous errors, Location could not be compiled and hence doesnt exist

h:\NSRM\Location.cpp(29): error C2065: 'x' : undeclared identifier
h:\NSRM\Location.cpp(31): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(33): error C2065: 'y' : undeclared identifier
h:\NSRM\Location.cpp(35): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(36): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(39): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(40): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(43): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(44): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(47): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(48): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Tile.cpp(6): error C2065: 'TheTileStatus' : undeclared identifier
h:\NSRM\Tile.cpp(6): error C2065: 'Sky' : undeclared identifier

-The rest of these should most likely be dealing with the previous errors, and will prolly need little adjusting after the first ones are fixed.