Sign in to follow this  
Monkan

Vector Class

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 once

class 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 product
float Vector3f::Dot(Vector3f &argVector)
{
return x * argVector.x + y * argVector.y + z * argVector.z;
}

// Cross product
Vector3f 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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this