I may be wrong. Your code may work without the copy constructor and stuff I added. It's good practice to always have a copy constructor, and something to learn.
I'm assuming this is one of the things you would like learn how to implement. Reply back if just a problem with trying to cout a void return type. And what compiler let you compile the cout << m.Location().Y(); ?
-
#include "Entity.h"
-
#include <iostream>
-
-
using namespace std;
-
-
int main(const int &)
-
{
-
Entity m;
-
m.Location().XY(2,15);
-
//cout << m.Location().Y();
-
[i]
-
// From the int getY() const; method you can
-
// learn from.
-
cout << m.Location().getY();
-
[i]
-
return 0;
-
}
Entity.h -
#include "Location.h"
-
-
class Entity
-
{
-
private:
-
GeoLocation mLocation;
-
public:
- // The "&" means pass by reference. This means that
-
// You are passing the object instead of copying to
-
// another GeoLocation object and passing that.
-
// If it gets copied to another GeoLocation object
-
// then passed, it calls the copy constructor, which
-
// you don't have. so the default constructor is called.
-
// Then a new GeoLocation was returned with only
-
// the default constructors called.
-
GeoLocation &Location();
-
-
// This makes program faster with less usage of memory
-
// because when passed by "&" does not require a copy
-
// constructor (GeoLocation x = (GeoLocation)y).
-
void Location(const GeoLocation &loc);
-
};
Entity.cpp - GeoLocation Entity::Location()
-
{
-
return mLocation;
-
}
-
-
// Parameter here needed changed as well...
-
void Entity::Location(const GeoLocation &location)
-
{
-
mLocation = location;
-
}
GeoLocation.cpp - class GeoLocation
-
{
-
private:
-
float mX;
-
float mY;
-
-
public:
-
GeoLocation()
-
{
-
mX = 0.0f;
-
mY = 0.0f;
-
}
-
- // Copy constructor. Tells how it is suppose to
-
// be copied. GeoLocation x = (GeoLocation)y;
-
// causes it to be used.
-
GeoLocation(const GeoLocation &loc)
-
{
-
// Makes sure that: GeoLocation x; x = x;
-
// Does not happen. Just prevents extra work.
-
if (&loc != this)
-
{
-
mX = loc.getX();
-
mY = loc.getY();
-
} // End if
-
}
-
// Must have for copy constructor. const so that
-
// copy constructor can access. getX does not
-
// change mX or mY or anything, so valid use of const
-
float getX() const
-
{
-
return(mX);
-
}
-
float getY() const
-
{
-
return(mY);
-
}
-
-
void X(float x)
-
{
-
mX = x;
-
}
-
void Y(float y)
-
{
-
mY = y;
-
}
-
void XY(int x, int y)
-
{
-
mX = (float)x;
-
mY = (float)y;
-
}
-
};
Changing GeoLocation Location();
to GeoLocation &Location(); should fix your problem alone.
This prevents a new object being returned, and passes the one returned instead.
Sort of like:
GeoLocation &Location()
{
Geolocation ret;
// Now that there is a copy constructor, this will work...
// But since there wasn't one, here's how it worked:
ret = mLocation; // No copy constructor, so ret simply
// initialized to mX = mY = 0;
return ret;
} // End Location() Function
This is how your function Location was working before. If copy constructors are a bit advanced for you, leave them out for now. Stick with returning references --ClassName &function()-- and having [const ClassName &var] parameters.
// As an example... basic data types such as int, float,
// double, long don't need to be passed or returned by
// reference... (they fit in one register in the processor).
ClassName1 &foofunction(const ClassName2 &var);
Also, look up how to do const functions. getX() I wrote is a good example. int getX() const; has const after it so that if an object gets passed into var through foofunction's (above) parameter, you can access it. var.getX() valid. var.X(5) invalid (because X changes data in GeoLocation, and therefore cannot be a const function).
Apply this to each function which takes in or returns a Class Object, respectively. Experiment by removing const in all functions. It will still work. But many times there are circumstances where something that uses your Class will be const, like text "hello", which is a const char * being passed to your constructor.
Keep programming. It's hard to get the fundamentals of c++, but once you do, the power of what you make is pushed speeds most other languages can't touch (assembly is an exception, but it's harder than c++)
(Sorry if I'm bad at explaining things. I love programming, but am not the best at explaining myself)