Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Linking error

Started by Jezper Aug 25, 2008 at 3:02 PM 0 replies 450+ views
Original Post
Jezper
Jezper
I've stared at this all day, and i just can't find what's wrong. This is what i have: vector.h:

template <class T>
class Vector3 {
public:
	T x, y, z;
	Vector3<T>(T nx = 0, T ny = 0, T nz = 0) : x(nx), y(ny), z(nz) {}
	Vector3<T>(const Vector3<T> &v) : x(v.x), y(v.y), z(v.z) {}
	.......
	void set(const T nx, const T ny, const T nz);
	...
};

vector.cpp:

template <class T> 
void Vector3<T>::set(const T nx, const T ny, const T nz)
{
	x = nx;
	y = ny;
	z = nz;
}

main.cpp:

	Vector3<float> vec1;
	vec1.set(1, 2, 3);

And i'm getting this: 1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Vector3::set(float,float,float)" (?set@?$Vector3@M@Cyb@@QAEXMMM@Z) referenced in function _main 1>D:\Projects\Cyb test\Debug\Cyb test.exe : fatal error LNK1120: 1 unresolved externals I'm using Visual studio 2008, wich i'm completley new to. What am i doing wrong?
MJP
MJP
With template classes, you have to define member functions in the header file. This is because template functions aren't compiled by default, they're compiled "on-demand" whenever an execution unit specifies that it wants a specific version of a template class. So for example when you create your Vector3 with a type of float in main.cpp, the compiler says "okay let's generate a version of Vector3 with floats" and at this point goes into vector.h and generates specialized versions of all the member functions it finds. Since your set method wasn't in the header file, a float version of it wasn't generated and compiled and therefore the linker couldn't find it.

Topic Locked

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

Sign in to reply to this topic.