Problem with return value of private variable within class
-
I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?
#include <iostream>
using namespace std;
class Point
{
public:
// constructors
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);// destructors (Only necessary for new dynamic memory created) ~Point(); float getX();
private:
float x, y, z;protected:
};
// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}// define destructor
Point::~Point()
{
cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}// public function to return private value of variable x
float Point::getX()
{
return x;
}int main()
{
// create instance of class
Point myLocation(23, 54, 32);// create another instance of class //Point anotherLocation; // calling constructor with args //Point thirdLocation(23, 14, 52); // return value of x cout << myLocation.getX() << endl; return 0;
}
-
I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?
#include <iostream>
using namespace std;
class Point
{
public:
// constructors
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);// destructors (Only necessary for new dynamic memory created) ~Point(); float getX();
private:
float x, y, z;protected:
};
// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}// define destructor
Point::~Point()
{
cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}// public function to return private value of variable x
float Point::getX()
{
return x;
}int main()
{
// create instance of class
Point myLocation(23, 54, 32);// create another instance of class //Point anotherLocation; // calling constructor with args //Point thirdLocation(23, 14, 52); // return value of x cout << myLocation.getX() << endl; return 0;
}
You are trying to get float variable X which has no assigned value. Point::Point(float f_x, float f_y, float f_z) { // Assign value to your variable x = f_x; y = f_y; z = f_z; cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl; }
-
You are trying to get float variable X which has no assigned value. Point::Point(float f_x, float f_y, float f_z) { // Assign value to your variable x = f_x; y = f_y; z = f_z; cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl; }
When I instantiate the object with this:
// create instance of class
Point myLocation(23, 54, 32);That should assign values 23, 54, 32 to x, y, and z, respectively.
-
When I instantiate the object with this:
// create instance of class
Point myLocation(23, 54, 32);That should assign values 23, 54, 32 to x, y, and z, respectively.
No. you are wrong here. If you debug your code you can see the wrong.
-
No. you are wrong here. If you debug your code you can see the wrong.
I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
-
I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
Michael Randolph wrote:
I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
I think you are seriously mistaken here: the compiler is no magic. You declare a constructor and pass two variables in that constructor. If you don't do anything with those values, the compiler won't magically assign them to your class members. That would be crazy... You have to assign them yourself as the other poster already told you.
Michael Randolph wrote:
I thought once you instantiate the object it sets the variables that I have for parameters in the function?
Once again, the compiler is no magic, it can't guess what you want to do with the variables. What if you pass less variables than you have class members (or more) ? What if they are in different order ? In most of the cases, a class is much more than two member variables (sometimes around 100 members) and in general you pass only a few of them in the constructor. So, how will the compiler know tho which member variables he has to assign those ? That would be crazy...
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?
#include <iostream>
using namespace std;
class Point
{
public:
// constructors
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);// destructors (Only necessary for new dynamic memory created) ~Point(); float getX();
private:
float x, y, z;protected:
};
// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}// define destructor
Point::~Point()
{
cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}// public function to return private value of variable x
float Point::getX()
{
return x;
}int main()
{
// create instance of class
Point myLocation(23, 54, 32);// create another instance of class //Point anotherLocation; // calling constructor with args //Point thirdLocation(23, 14, 52); // return value of x cout << myLocation.getX() << endl; return 0;
}
Change the constructor to assign the constructor parameters to the members, either using initialiser syntax:
Point::Point(float f_x, float f_y, float f_z) : x(f_x), y(f_y), z(f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}or explicit assignments
Point::Point(float f_x, float f_y, float f_z)
{
x = f_x;
y = f_y;
z = f_z;
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}As Cedric said - the compiler ain't magic - it doesn't look for similarities between parameter names and data member names and decide to assign parameters to data members automatically!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
-
Change the constructor to assign the constructor parameters to the members, either using initialiser syntax:
Point::Point(float f_x, float f_y, float f_z) : x(f_x), y(f_y), z(f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}or explicit assignments
Point::Point(float f_x, float f_y, float f_z)
{
x = f_x;
y = f_y;
z = f_z;
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}As Cedric said - the compiler ain't magic - it doesn't look for similarities between parameter names and data member names and decide to assign parameters to data members automatically!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
ahh I see. That makes so much more sense! Thanks for your responses. My next question deals with inheritance. It appears that the = operator declaration is not being inherited from my class Point. I am getting this compiling error:
g++ -o inheritance inheritance.cpp
inheritance.cpp: In function ‘int main()’:
inheritance.cpp:165: error: no match for ‘operator=’ in ‘vect3 = Vector::operator+(Vector&)(((Vector&)(& vect2)))’
inheritance.cpp:135: note: candidates are: Vector& Vector::operator=(Vector&)here is my source:
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();// return by reference void getXYZ(float &X, float &Y, float &Z); float getX(); float getY(); float getZ(); void setXYZ(float X, float Y, float Z); void setX(float X); void setY(float Y); void setZ(float Z); // overloading = operator to deal with pointers Point operator = (Point &p);
private:
float x, y, z;protected:
};
// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
x = f_x;
y = f_y;
z = f_z;
}// define destructor
Point::~Point()
{
cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}// public function to return private value of variable x
float Point::getX()
{
return x;
}// public function to return private value of variable y
float Point::getY()
{
return y;
}// public function to return private value of variable z
float Point::getZ()
{
return z;
}// public function to change value of private variable x
void Point::setX(float X)
{
x = X;
}// public function to change value of private variable y
void Point::setY(float Y)
{
y = Y;
}// public function to change value of private variable z
void Point::setZ(float Z)
{
z = Z;
}// public function to change value of all three private variables
void Point::setXYZ(float X, float Y, float Z)
{
/* slower
setX(X);
setY(Y);
setZ(Z);*/
// faster
x = X;
y = Y;
z = Z;}
// public function to return values of x,y,z by reference
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}// overloading operator << (output stream)
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() < -
ahh I see. That makes so much more sense! Thanks for your responses. My next question deals with inheritance. It appears that the = operator declaration is not being inherited from my class Point. I am getting this compiling error:
g++ -o inheritance inheritance.cpp
inheritance.cpp: In function ‘int main()’:
inheritance.cpp:165: error: no match for ‘operator=’ in ‘vect3 = Vector::operator+(Vector&)(((Vector&)(& vect2)))’
inheritance.cpp:135: note: candidates are: Vector& Vector::operator=(Vector&)here is my source:
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();// return by reference void getXYZ(float &X, float &Y, float &Z); float getX(); float getY(); float getZ(); void setXYZ(float X, float Y, float Z); void setX(float X); void setY(float Y); void setZ(float Z); // overloading = operator to deal with pointers Point operator = (Point &p);
private:
float x, y, z;protected:
};
// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
x = f_x;
y = f_y;
z = f_z;
}// define destructor
Point::~Point()
{
cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}// public function to return private value of variable x
float Point::getX()
{
return x;
}// public function to return private value of variable y
float Point::getY()
{
return y;
}// public function to return private value of variable z
float Point::getZ()
{
return z;
}// public function to change value of private variable x
void Point::setX(float X)
{
x = X;
}// public function to change value of private variable y
void Point::setY(float Y)
{
y = Y;
}// public function to change value of private variable z
void Point::setZ(float Z)
{
z = Z;
}// public function to change value of all three private variables
void Point::setXYZ(float X, float Y, float Z)
{
/* slower
setX(X);
setY(Y);
setZ(Z);*/
// faster
x = X;
y = Y;
z = Z;}
// public function to return values of x,y,z by reference
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}// overloading operator << (output stream)
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() <OK - a couple of issues there. Firstly, your
operator=
isn't inherited because the signature ofoperator=
in Point doesn't conform to what the generatedoperator=
in Vector wants to see - it wants this:Point& operator = (Point const &p);
i.e. take a const reference and return a non-const reference. Having said that, returning Point by value rather than reference is also a conformant signature, as C++ function signatures depend on parameter types and CV qualification (for methods), not return value - but why return Point by value anyway? Secondly, make your getters const methods - then they can be used on const references, as (now) taken by your
operator=
:void getXYZ(float &X, float &Y, float &Z) const; float getX() const; float getY() const; float getZ() const;
If you make those two changes, then your Vector's generated
operator=
will compile successfully.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
OK - a couple of issues there. Firstly, your
operator=
isn't inherited because the signature ofoperator=
in Point doesn't conform to what the generatedoperator=
in Vector wants to see - it wants this:Point& operator = (Point const &p);
i.e. take a const reference and return a non-const reference. Having said that, returning Point by value rather than reference is also a conformant signature, as C++ function signatures depend on parameter types and CV qualification (for methods), not return value - but why return Point by value anyway? Secondly, make your getters const methods - then they can be used on const references, as (now) taken by your
operator=
:void getXYZ(float &X, float &Y, float &Z) const; float getX() const; float getY() const; float getZ() const;
If you make those two changes, then your Vector's generated
operator=
will compile successfully.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thank you so much for your help. Looks like I have a bit of studying to do. I am not totally sure how this works but it does. Here is the fixed source in case someone needs to check it out.
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();void setXYZ(float X, float Y, float Z); void setX(float X); void setY(float Y); void setZ(float Z); // return by reference void getXYZ(float &X, float &Y, float &Z); float getX() const; float getY() const; float getZ() const; // overloading = operator to deal with pointers Point& operator =(Point const &p);
private:
float x, y, z;protected:
};
// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
x = f_x;
y = f_y;
z = f_z;
}// define destructor
Point::~Point()
{
cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}// public function to return private value of variable x
float Point::getX() const
{
return x;
}// public function to return private value of variable y
float Point::getY() const
{
return y;
}// public function to return private value of variable z
float Point::getZ() const
{
return z;
}// public function to change value of private variable x
void Point::setX(float X)
{
x = X;
}// public function to change value of private variable y
void Point::setY(float Y)
{
y = Y;
}// public function to change value of private variable z
void Point::setZ(float Z)
{
z = Z;
}// public function to change value of all three private variables
void Point::setXYZ(float X, float Y, float Z)
{
/* slower
setX(X);
setY(Y);
setZ(Z);*/
// faster
x = X;
y = Y;
z = Z;}
// public function to return values of x,y,z by reference
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}// overloading = operator
Point& Point::operator =(Point const &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());
// passing back value rather than address
return *this;
}// overloading operator << (output stream)
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() << " " << p.getY() << " " << p.getZ();
return stream;
}// overloading operator >> (input