# Vector Class

This topic is 2874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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);

Thanks

##### Share on other sites
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!

##### Share on other sites
Cool cool, thanks. I forgot I put that there. Thanks!

##### Share on other sites
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.)

##### Share on other sites
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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 88
• 11
• 10
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009618
• ### Who's Online (See full list)

There are no registered users currently online

×