   c++ template problem
  Whats the difference between vect2<float> f()andvect2<float> fBecause I'm getting an error like this undefined reference to "ncs::math::Vector2<float>::Vector2()"when I use the latter but if the other one it compiles.I arrange my code like this//ncsmath.h#ifndef __MATH__H__#define __MATH__H__namespace ncs {   namespace math{ template<class T> class Vector2{public:    Vector2();    Vector2( T a, T b );    T x, y; };   }}//ncsmath.cpp#include "ncsmath.h"template<class T>ncs::math::Vector2<T>::Vector() {    x = 0;    y = 0;}template<class T>ncs::math::Vector2<T>::Vector( T a, T b ) {    x = a;    y = b;}//main.cpp#include "ncsmath.h"int main( int argc, char*argv[] ) {    ncs::math::Vector2<int> f;    return 0;}By the way I'm using code::block and gcc compiler. What could be the problem in my code?   
Quote:Whats the difference between vect2<float> f()andvect2<float> fThe first one defines a function, f(), with a return type of vect2<float>. The second one declares a variable, f, of type vect2<float>.Also, when you write the definitions for the constructors, your using Vector, when the class is Vector2. Try changing ncs::math::Vector2<T>::Vector() to ncs::math::Vector2<T>::Vector2() and ncs::math::Vector2<T>::Vector( T a, T b ) to ncs::math::Vector2<T>::Vector2( T a, T b )   ;  1) you can't seperate templates the way you did.//ncsmath.cpp#include "ncsmath.h"template<class T>ncs::math::Vector2<T>::Vector() {    x = 0;    y = 0;}template<class T>ncs::math::Vector2<T>::Vector( T a, T b ) {    x = a;    y = b;}has to go in the header, because you are making a "template" it needs all the code available in the definition itself, so that it can put any type "T" in there.also, i think you meant "ncs::math::Vector2<T>::Vector2()"   ;  Okay thanks. So I was just declaring a prototype that's why it compiles. So whats wrong in my code that causes gcc to complains of undefined reference? By the way I tried putting all the code in the main.cpp and it works fine.   ;Quote:So whats wrong in my code that causes gcc to complains of undefined reference?This just means that the compile found all the right parts of the declaration, but not the definition.Finding the declaration of: template<class T> class Vector2{public:    Vector2();means you can call "Vector2<float> X;" in code and have it compile, but it won't link unless it can also findtemplate<class T>ncs::math::Vector2<T>::Vector2() {    x = 0;    y = 0;}Since, it is a template, you need the template definition to also be available from the header where you put the declaration.If it was a NON-Template function, you'd just have to have it in some .cpp file that was also linked into the project.;  @KulSeranThanks I get it now. I thought I could separate them because I wanted to hide all those so the header is a little neater.   ;Quote:Original post by GlaiveVehemI thought I could separate them because I wanted to hide all those so the header is a little neater.Some people prefer put the implementation into a second header file and give it some extension like .impl.hpp to indicate it stores only implementation code. This file is then included by your .hpp. This change is purely cosmetic of course, but that's better than nothing.   