Sign in to follow this  
GlaiveVehem

c++ template problem

Recommended Posts

GlaiveVehem    114
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 this post


Link to post
Share on other sites
Alatar    303
Quote:
Whats the difference between

vect2<float> f()

and

vect2<float> 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 this post


Link to post
Share on other sites
KulSeran    3267
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 this post


Link to post
Share on other sites
GlaiveVehem    114
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 this post


Link to post
Share on other sites
KulSeran    3267
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 this post


Link to post
Share on other sites
ComicSansMS    138
Quote:
Original post by GlaiveVehem
I 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this