Vector Class

Started by
3 comments, last by Monkan 13 years, 4 months ago
Hi,

I'm getting a bit confused with my vector class that I made and thought I understood but obviously not. The problem comes with the operator use I think but I just followed a tutorial on t'internet.

Vector3.h
#pragma onceclass Vector3f{public:	float x;	float y;	float z;	// Constructors	Vector3f(void);	Vector3f(float argX, float argY, float argZ);	~Vector3f(void); // Destructor	// Copy constructor	Vector3f(const Vector3f &argVector);		void operator = (const Vector3f &argVector)	{		x = argVector.x;		y = argVector.y;		z = argVector.z;			}		void operator += (const Vector3f &argVector)	{		x += argVector.x;		y += argVector.y;		z += argVector.z;	}	void operator -= (const Vector3f &argVector)	{		x -= argVector.x;		y -= argVector.y;		z -= argVector.z;	}	//Vector - Vector operators	Vector3f operator + (const Vector3f &argVector) const 	{		return Vector3f(x + argVector.x, y + argVector.y, z + argVector.z);	}	Vector3f operator - (const Vector3f &argVector) const 	{		return Vector3f(x - argVector.x, y - argVector.y, z - argVector.z);	}	Vector3f operator / (const Vector3f &argVector) const 	{		return Vector3f(x / argVector.x, y / argVector.y, z / argVector.z);	}		Vector3f operator * (const Vector3f &argVector) const 	{		return Vector3f(x * argVector.x, y * argVector.y, z * argVector.z);	}	//Vector - Float operators	Vector3f operator * (float argScale) const 	{		return Vector3f(x * argScale, y * argScale, z * argScale);	}	Vector3f operator / (float argScale) const 	{		return Vector3f(x / argScale, y / argScale, z / argScale);	}		// Dot product	float Dot(Vector3f &argVector);	//Cross	Vector3f Cross(Vector3f &argVector);	float Length();				void Normalise();	void PrintVector();};


Vector3.cpp
using namespace std;Vector3f::Vector3f(void){	x = 0;	y = 0;	z = 0;}Vector3f::Vector3f(float argX, float argY, float argZ){	x = argX;	y = argY;	z = argZ;}Vector3f::~Vector3f(void){}// Dot productfloat Vector3f::Dot(Vector3f &argVector){	return x * argVector.x + y * argVector.y + z * argVector.z;}// Cross productVector3f Vector3f::Cross(Vector3f &argVector){	return Vector3f(		y * argVector.z - z * argVector.y, 		z * argVector.x - x * argVector.z,		x * argVector.y - y * argVector.x);}void Vector3f::Normalise(){	x = x / Length();	y = y / Length();	z = z / Length();}float Vector3f::Length(){	return sqrt(x * x + y * y + z * z);}void Vector3f::PrintVector(){	cout<< x << ", " << y << ", " << z << endl;}


And this is the error I sometimes get:

error LNK2019: unresolved external symbol "public: __thiscall Vector3f::Vector3f(class Vector3f const &)" (??0Vector3f@@QAE@ABV0@@Z) referenced in function........
fatal error LNK1120: 1 unresolved externals

Sometimes I get problems and it seems that if I put a & in front of the vector name then it goes away and sometimes I end up creating a new vector by just calling all the vectors x, y, zs (not ideal really!).

Is the the way I am declaring them maybe?? at the moment I normally just declare them as normal i.e. Vector3f mooVec = Vector3f(x, y, z);

Please help!!

Thanks

"To know the road ahead, ask those coming back."

Advertisement
You declared, but didn't define, a copy constructor. Vector is a simple type, you can remove the copy constructor (and destructor) and let the compiler generate reasonable defaults. Don't write code if you don't need to!
Cool cool, thanks. I forgot I put that there. Thanks!

"To know the road ahead, ask those coming back."

If you're interested in (possibly) improving the class a bit, you might search the forums for e.g. 'vector class', 'vector class review', or 'vector class feedback'. There's a number of issues that have been discussed in previous, similar threads that are probably relevant here as well. (A particularly relevant issue here is member vs. non-member functions. Another relevant issue is code reuse.)
Cheers jyk,

Some of the stuff Ive found here is good brain fodder. I love the way there are so many different approaches to do the same thing, some real nuggets of interest.

"To know the road ahead, ask those coming back."

This topic is closed to new replies.

Advertisement