Skip to main content
gamedev.net
🔒 Locked

Strange Linking error

Started by TheSteve 2 replies 700+ views
Original Post
TheSteve
TheSteve
I'm trying to design a vector class for my app. In testing it, whenever I try to do the line myvector3 = myvector plus myvector2 in the app I get this error: Error: Math error LNK2019: unresolved external symbol "public: __thiscall ShyrVec4::ShyrVec4(float,float,float,float)" (??0ShyrVec4@@QAE@MMMM@Z) referenced in function "public: class ShyrVec4 __thiscall ShyrVec4::operator+(class ShyrVec4 const &)const " (??HShyrVec4@@QBE?AV0@ABV0@@Z) Now, here's the full code for my header. however, I think that the plus operator is the problem. I've been having some weird issues with the plus symbol being eaten in this bbs, so if you can't see the symbol I'll try and repost it. (it's the third operator defined, btw)
[source lange="cpp]
#include <math.h>

class ShyrVec4
{
public:
	float x, y, z, w;

	ShyrVec4() {};
	ShyrVec4(const float x, const float y, const float z, const float w);
	void	Set(const float x, const float y, const float z);
	
	ShyrVec4	Cross( const ShyrVec4 &a ) const;
	ShyrVec4	⨯( const ShyrVec4 &a, const ShyrVec4 &b );
	
	float	Length(const float x, const float y, const float z);
	void	Zero();

	float	operator[]( const int index ) const;
	float	&operator[]( const int index );

	ShyrVec4 &operator =(const ShyrVec4 &a);

	ShyrVec4 operator +(const ShyrVec4  &a) const;
	ShyrVec4 operator -(const ShyrVec4 &a);

	ShyrVec4 &operator +=(const ShyrVec4 &a);
	ShyrVec4 &operator -=(const ShyrVec4 &a);
	ShyrVec4 &operator *=(const ShyrVec4 &a);

	//int		 operator ==(const ShyrVec4 &a);
	//int		 operator !=(const ShyrVec4 &a);

	//Dot Product 
	float	 operator *(const ShyrVec4 &a);
	//Scalar Multiplication
	ShyrVec4 operator *(const float a);
};


inline float ShyrVec4::operator[]( const int index ) const {
	return ( &x )[ index ];
}

inline float &ShyrVec4::operator[]( const int index ) {
	return ( &x )[ index ];
}
inline ShyrVec4 ShyrVec4::operator +(const ShyrVec4 &a) const
{
	return ShyrVec4(x + a.x, y + a.y, z + a.z, 1);
}

inline ShyrVec4 ShyrVec4::operator -(const ShyrVec4 &a)
{
	return ShyrVec4(x - a.x, y - a.y, z - a.z, 1);
}

inline ShyrVec4 &ShyrVec4::operator -=(const ShyrVec4 &a)
{
	x -= a.x;
	y -= a.y;
	z -= a.z;
	return *this;
}

inline ShyrVec4 &ShyrVec4::operator +=(const ShyrVec4 &a)
{
	x += a.x;
	y += a.y;
	z += a.z;
	return *this;
}

inline ShyrVec4 &ShyrVec4::operator *=(const ShyrVec4 &a)
{
	x *= a.x;
	y *= a.y;
	z *= a.z;
	return *this;
}

inline float ShyrVec4::operator *(const ShyrVec4 &a)
{
	return x *a.x + y * a.y + z * a.z;
}

inline ShyrVec4 ShyrVec4::operator *(float a)
{
	return ShyrVec4(x * a, y * a, z * a, 1);
}

inline ShyrVec4 &ShyrVec4::operator =(const ShyrVec4 &a)
{
	x = a.x;
	y = a.y;
	z = a.z;
	return *this;
}

inline float ShyrVec4::Length(const float x, const float y, const float z)
{
	float temp;
	temp = x*x + y*y + z*z;
	return sqrt(temp);
}



inline void ShyrVec4::Zero()
{
	this->x = 0;
	this->y = 0;
	this->z = 0;

}
inline void ShyrVec4::Set(const float x, const float y, const float z)
{
	this->x = x;
	this->y = y;
	this->z = z;
	this->w = 1;

}
inline ShyrVec4 ShyrVec4::Cross( const ShyrVec4 &a ) const {
	return ShyrVec4( y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x, 1 );
}

inline ShyrVec4 &ShyrVec4::Cross( const ShyrVec4 &a, const ShyrVec4 &b ) {
	x = a.y * b.z - a.z * b.y;
	y = a.z * b.x - a.x * b.z;
	z = a.x * b.y - a.y * b.x;

	return *this;
}

And, of course, here's the simple main function I'm using to test the class:
[source lang="cpp]

int main ()
{
	ShyrVec4 myvector;
	ShyrVec4 myvector2;
	ShyrVec4 myvector3;
	char x;


	myvector2.Set(1,2,3);
	myvector.Set(1, 2, 3);
	
	myvector3 = myvector; //This works
	
	myvector3 = myvector + myvector2; // this is the line that gives the error

	cout<<myvector3.x<<myvector3.y<<myvector3.z; // works without the previous line
	cin>>x;
	return 1;
}

I haven't done a lot of operator overloading in the past, so I think I may have done something wrong. Any help is really appreciated! cheers!
stylin
stylin
Looking over your code, I don't see you defining

   ShyrVec4(const float x, const float y, const float z, const float w);

anywhere. This should take care of your linker problem.

EDIT: Also, a few critiques:

You've got a set(...) method with public data members; seems unnecessary.

You've got your + and - operators returing a value vector. These should be const-ified to prevent accidental "I-meant-equality-not-assignment" errors (your custom types, especially numeric-based, should emulate built-in type behavior wherever possible).

Personally, I'd take all your mathematical functions out of the class and implement them as helper functions. (try multiplying a scalar by a vector, and see if it compiles)
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
JohnBolton
JohnBolton
The error says you are missing the function ShyrVec4::ShyrVec4(float,float,float,float) (thats what "unresolved external symbol" means). Now, you declared the function in the class definition, but did you actually implement it anywhere?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
TheSteve
TheSteve
This never happened.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.