Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Template issues

Started by evilops Dec 27, 2004 at 12:05 AM 4 replies 800+ views
Original Post
evilops
evilops
Hey all. I've made some pretty simple vector template classes for 2, 3 and 4 elements, with overloaded operators and a couple of extra methods. Then with a few typedef calls I've got int2, float2, int3, etc classes defined.

template <class T> 
class vector2 {
public:
	vector2() { x=0; y=0; }
	vector2(const T &nx, const T &ny) { x=nx; y=ny; }

	vector2<T> &operator=(const vector2<T> &n) { x=n.x; y=n.y; return *this; }
	
        // ...

	T x,y;
};

typedef vector2<int> int2;
typedef vector2<float> float2;

The problem comes when trying to for eg. assign a float2 to an int2. I want to be able to overload the copy constructor for specific types (T) as well as others but I think i've reached a limitiation of templates... For addition and subtraction of vector2s of different types I can globally overload the (+|-) operators, but I can't globally overload the (=) operator. Template specialisations could be made so I could overload the (=) operator inside the class but it would defeat the purpose of using templates, because I'd have to write ALL of the member functions into the specialised template (as far as I know). Does anyone know a way around this? Thanks, Brendan.
SiCrane
SiCrane
Provided your compiler groks them, you can try using templated member function overloads:
template <class T> class vector2 {public:	vector2() { x=0; y=0; }	vector2(const T &nx, const T &ny) { x=nx; y=ny; }	vector2<T> &operator=(const vector2<T> &n) { x=n.x; y=n.y; return *this; }	        template <typename U>        vector2<T> &operator=(const vector2< U > &n) { x=n.x; y=n.y; return *this; }        // ...	T x,y;};
Drew_Benton
Drew_Benton
SiCrane is correct. I tried it out on VS7 - but note that all this template stuff will not work on VS6. You can also use the same concept to make an overload copy constructor as well - it accompishes the same thing. I had a post made about that, then I realized it was similar to SiCrane's. Darn his response time [smile].

Here is a great site I used for learning some crazy stuff you can do with templates, b/c up until yesterday - I didn't know you could make templated functions!
evilops
evilops
Ditto about template functions, didn't know much about templates myself until yesterday. Cheers for the help! But there's another small problem. The code compiles now (I added in overloaded template functions for +|- also) but there are linker warnings, yet the program still runs using /FORCE. The linker warnings are a mass spamming of the following (a lot of files include this "arraytypes.h" file):

engine warning LNK4006: "class vector2 const __cdecl operator-(class vector2 const &,class vector2 const &)" (??G@YA?BV?$vector2@M@@ABV0@ABV?$vector2@H@@@Z) already defined in arraytypes.obj; second definition ignored

template <class T> class vector2 {public:	vector2() { x=0; y=0; }	vector2(const T &nx, const T &ny) { x=nx; y=ny; }	vector2<T> &operator=(const vector2<T> &n) { x=n.x; y=n.y; return *this; 	template <class U> vector2<T> &operator=(const vector2 &n) { x=n.x; y=n.y; return *this; }	const vector2<T> operator+(const vector2<T> &n) const { return vector2<T>(x+n.x, y+n.y); }	const vector2<T> operator-(const vector2<T> &n) const { return vector2<T>(x-n.x, y-n.y); }	const vector2<T> operator*(const T &n) const { return vector2<T>(x*n, y*n); }	const vector2<T> operator/(const T &n) const { return vector2<T>(x/n, y/n); }	template <class U> const vector2<T> operator+(const vector2 &n) const { return vector2<T>(x+n.x, y+n.y); }	template <class U> const vector2<T> operator-(const vector2 &n) const { return vector2<T>(x-n.x, y-n.y); }        //...	T x,y;};


any ideas?
thanks :)
SiCrane
SiCrane
Off the top of my head, it looks like you didn't declare a non-member operator overload to be inlined and included the full body of the function in a header file. The fix would be to declare the function inline.
evilops
evilops
Thanks for the quick reply, turns out I just didn't remove the globally overloaded methods I made for the +|- operators before. *slaps self*

Also Drew that site is awesome, some good stuff that you don't find in the regular tutorials. The pointer to member function stuff is fairly obvious but something I didn't know about until now.

Topic Locked

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

Sign in to reply to this topic.