Do the functions have to be in a .h file? I have just tried to add the class definition in a .h file and then the functions in the .cpp file and I can't get it to work. Isn't it supposed to work?
ØØ
Printable View
Do the functions have to be in a .h file? I have just tried to add the class definition in a .h file and then the functions in the .cpp file and I can't get it to work. Isn't it supposed to work?
ØØ
Just for the record. Here is the code, maybe there is something else wrong. I have only done this once before, and that is 2 years ago now, so please don't beat me to deth if it is obvious....:D
.h file
Code:#ifndef _CVECTOR3_H
#define _CVECTOR3_H
// This is our basic 3D point/vector class
class CVector3{
public:
float x;
float y;
float z;
// A default constructor
CVector3();
// This is our constructor that allows us to initialize our data upon creating an instance
CVector3(float X, float Y, float Z);
// Here we overload the + operator so we can add vectors together
CVector3 operator+(CVector3 vVector);
// Here we overload the - operator so we can subtract vectors
CVector3 operator-(CVector3 vVector);
// Here we overload the * operator so we can multiply by scalars
CVector3 operator*(float num);
// Here we overload the / operator so we can divide by a scalar
CVector3 operator/(float num);
};
#endif
.cpp file
Code:#include "cvector3.h"
// A default constructor
CVector3::CVector3() {
}
// This is our constructor that allows us to initialize our data upon creating an instance
CVector3::CVector3(float X, float Y, float Z) {
x = X; y = Y; z = Z;
}
// Here we overload the + operator so we can add vectors together
CVector3 CVector3::CVector3 operator+(CVector3 vVector){
// Return the added vectors result.
return CVector3(vVector.x + x, vVector.y + y, vVector.z + z);
}
// Here we overload the - operator so we can subtract vectors
CVector3 CVector3::CVector3 operator-(CVector3 vVector){
// Return the subtracted vectors result
return CVector3(x - vVector.x, y - vVector.y, z - vVector.z);
}
// Here we overload the * operator so we can multiply by scalars
CVector3 CVector3::CVector3 operator*(float num){
// Return the scaled vector
return CVector3(x * num, y * num, z * num);
}
// Here we overload the / operator so we can divide by a scalar
CVector3 CVector3::CVector3 operator/(float num){
// Return the scale vector
return CVector3(x / num, y / num, z / num);
}
and the errors that I am getting. It looks like it can't find the member variables, and the two last warning kind of concerns me...:(
http://vbforums.com/attachment.php?s=&postid=1828294
Shoot me in my nuts, and then laugh of me...:D
How many times did I write CVector3 on each line in the Cpp file...:D
REsolved...:D
Pass the arguments to the operators as const references. Use an initializer list in the argument constructor. And initialize the components to 0 in the default constructor, having some constructors leave the object in an undefined state and others not is usually not a good idea.
Quote:
Originally posted by CornedBee
Pass the arguments to the operators as const references. Use an initializer list in the argument constructor. And initialize the components to 0 in the default constructor, having some constructors leave the object in an undefined state and others not is usually not a good idea.
I changed all the parameters to const refs after I posted this. ANd I am never using the default constructor, so I don't see the point, but maybe I will use the class an other time too, so why not.
Using an initialisation list was not in my mind? Why do I have to do it in this example. Is it faster, or is there something else I have forgotten about this?
Leaving bad or unpredictable code "because I don't use it anyway" is a wonderful source for future bugs.
There are two reasons why you should do it in this example. First, to get into the habit. Second, because it may allow the compiler to do optimizations it can not or will not use otherwise.Quote:
Why do I have to do it in this example. Is it faster, or is there something else I have forgotten about this?
Quote:
Originally posted by CornedBee
Leaving bad or unpredictable code "because I don't use it anyway" is a wonderful source for future bugs.
Yeah thats why I wrote the "but maybe I will use the class an other time too, so why not. " part....:)
Quote:
Originally posted by CornedBee
There are two reasons why you should do it in this example. First, to get into the habit. Second, because it may allow the compiler to do optimizations it can not or will not use otherwise.
I know I have a bad habbit of leaving it out. Probably because I never remember when I can do it, and when not to. Well I will do it now, and try to remember it next time. Thanks for very inteligent answers...:)