# c++ template problem

This topic is 2861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Whats the difference between
vect2<float> f()
and
vect2<float> f
Because 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?

##### Share on other sites
Quote:
 Whats the difference between vect2 f()andvect2 f

The 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 )

##### Share on other sites
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()"

##### Share on other sites
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.

##### Share on other sites
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 find
template<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.

##### Share on other sites
@KulSeran
Thanks I get it now. I thought I could separate them because I wanted to hide all those so the header is a little neater.

##### Share on other sites
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.