I've been doing some work in C++ for a few months now, but only just had the requirement to design and implement some new classes. I didn't think that would be a problem, however I think I'm taking an incorrect approach for implementing classes, and was wondering if anyone could help me?
My problem, the value I return is not the same as the value I set. Not surprisingly the memory address that the value gets assigned to, doesn't match the one I'm getting the value back from either...
[CODE=cpp]
#include "Entity.h"
#include <iostream>
using namespace std;
int main(const int &)
{
Entity m;
m.Location().XY (2,15);
cout << m.Location().Y( );
return 0;
}[/CODE]
Entity.h
[CODE=cpp]
#include "Location.h "
class Entity
{
private:
GeoLocation mLocation;
public:
GeoLocation Location();
void Location(GeoLoc ation loc);
};[/CODE]
Entity.cpp
[CODE=cpp]GeoLocation Entity::Locatio n()
{
return mLocation;
}
void Entity::Locatio n(GeoLocation location)
{
mLocation = location;
}[/CODE]
GeoLocation.cpp
[CODE=cpp]class GeoLocation
{
private:
float mX;
float mY;
public:
GeoLocation()
{
mX = 0.0f;
mY = 0.0f;
}
void X(float x)
{
mX = x;
}
void Y(float y)
{
mY = y;
}
void XY(int x, int y)
{
mX = (float)x;
mY = (float)y;
}
};[/CODE]
Output:
0
Any help would be much appreciated. I'm sure it will be something simple, I'm just not quite sure what it is. Thanks.
Ian
My problem, the value I return is not the same as the value I set. Not surprisingly the memory address that the value gets assigned to, doesn't match the one I'm getting the value back from either...
[CODE=cpp]
#include "Entity.h"
#include <iostream>
using namespace std;
int main(const int &)
{
Entity m;
m.Location().XY (2,15);
cout << m.Location().Y( );
return 0;
}[/CODE]
Entity.h
[CODE=cpp]
#include "Location.h "
class Entity
{
private:
GeoLocation mLocation;
public:
GeoLocation Location();
void Location(GeoLoc ation loc);
};[/CODE]
Entity.cpp
[CODE=cpp]GeoLocation Entity::Locatio n()
{
return mLocation;
}
void Entity::Locatio n(GeoLocation location)
{
mLocation = location;
}[/CODE]
GeoLocation.cpp
[CODE=cpp]class GeoLocation
{
private:
float mX;
float mY;
public:
GeoLocation()
{
mX = 0.0f;
mY = 0.0f;
}
void X(float x)
{
mX = x;
}
void Y(float y)
{
mY = y;
}
void XY(int x, int y)
{
mX = (float)x;
mY = (float)y;
}
};[/CODE]
Output:
0
Any help would be much appreciated. I'm sure it will be something simple, I'm just not quite sure what it is. Thanks.
Ian
Comment