-1

I have a problem with codeblocks ver 10.05. I made a c++ project, and I wrote a program like this:

main.cpp

#include <iostream>
#include "vectorddd.hpp"


using namespace std;

int main()
{

    vector3D<int> tesztinttomb;
    tesztinttomb.saveout("igen.dat");
    return 0;
}

header file (vectorddd.hpp):

#ifndef VECTORDDD_HPP_INCLUDED
#define VECTORDDD_HPP_INCLUDED

#include <iostream>

template <class T>
class vector3D  {
    T *x;
    T *y;
    T *z;
    int meret;
public:
    void saveout(char* FileName);

    vector3D(int Meret=0) :  x(new T[meret]), y(new T[Meret]), z(new T[Meret]), meret(Meret) {}

    ~vector3D()  { delete [] x; delete [] y; delete [] z; }
};


#endif // VECTORDDD_HPP_INCLUDED

implementation file (vectorddd.cpp):

#include "vectorddd.hpp"

template <class T>
void vector3D<T>::saveout(char* FileName) {
    int i=0;// I know this is stupid... but the emphasis is on the linking problem

}

And it just doesn't link together. I know I have to check .cpp files link and compile settings at properties->build options. And I don't find any problem, but is just write always the same:

In function `main':
undefined reference to `vector3D<int>::saveout(char*)'
||=== Build finished: 1 errors, 0 warnings ===|

And if I put the .cpp files implementations into my .hpp file it works correctly. But this is not how codeblocks should work.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
Marcell
  • 11
  • 3
  • 5
    [Template definitions must go in headers](http://www.parashift.com/c++-faq/templates.html#faq-35.13), this has nothing to do with CB. – ildjarn Apr 19 '12 at 19:39
  • http://stackoverflow.com/q/115703/1214731 Good reading here for more info – tmpearce Apr 19 '12 at 19:40

1 Answers1

2

Your templates need to be in your header files, think about it, how can the templates be instantiated if they are in the cpp files?

You should put this:

template <class T>
void vector3D<T>::saveout(char* FileName) {
    int i=0;// I know this is stupid... but the emphasis is on the linking problem

}

in your header vectorddd.hpp file

see similar SO post: Storing C++ template function definitions in a .CPP file

Community
  • 1
  • 1
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • What do you mean, my function doesn't have teamplate memeber – Marcell Apr 19 '12 at 19:49
  • Why can't it be instantiated, if it is in cpp? It is linked together, and then if the compiler sees, there is a function called, then it just instatniate one from the function implementation. – Marcell Apr 19 '12 at 19:53
  • I can't believe this just doesn't work like this. – Marcell Apr 19 '12 at 19:54
  • So i can't put the teamplate class functions's implementation into the cpp file? o.O – Marcell Apr 19 '12 at 19:54
  • @Marcell Yes but it's templated code, where is the definition if it is not in the header? If I include vectorddd.hpp and I have to use `saveout` and you'ved defined it as a template, how do I know how to instantiate the function if it is not in the header? This is the whole point of templates. – EdChum Apr 19 '12 at 19:59
  • @Marcell Also you should read this C++ faq: http://www.parashift.com/c++-faq/templates.html#faq-35.13 – EdChum Apr 19 '12 at 20:00
  • So I will be never able to do this:? : template friend const vector3D operator-(const B& egyik, const vector3D& vector); – Marcell Apr 19 '12 at 20:11
  • @Marcell No absolutely not, think of templated code almost like a macro or where your templated code will be copied and pasted with class T substituted with your type, in your case int, if the caller does not have access to the code (remember no code is generated just because you declared some templated code) then how can they instantiate all the templated code such as `saveout`? – EdChum Apr 19 '12 at 20:14
  • The codeblocks accepted a code like this: This is just a part of tha code: vectorddd.hpp http://pastebin.com/iuz7f8dn – Marcell Apr 19 '12 at 20:21
  • Thank how could it just accept the code like this if the site that you liked to me is right? o.O – Marcell Apr 19 '12 at 20:22
  • @Marcell well it tells me that codeBlocks is wrong or your code is not portable this is not how it should work – EdChum Apr 19 '12 at 20:23
  • By the way, if i think of a template that is a macro, than i can immagine how the instance will look like from the above information. And it has only one way, so the question is that what couldn't you quess from this source? One think must be known, when yuo call a function like this: – Marcell Apr 19 '12 at 20:24
  • i have a vector3D intvetor; and i have an int intege; than if i write a code like this: – Marcell Apr 19 '12 at 20:25
  • intvector= intege + intvector; then it can call the funciton like this: const vector3D operator-(const B& egyik, const vector3D& vector) [class A= int, class B=int] – Marcell Apr 19 '12 at 20:27
  • The only thing that i can't understand, why i have to do the implementation in the hpp file, why it doesn't accept it in the cpp? So why only in the hpp? – Marcell Apr 19 '12 at 20:28
  • Anyhow, thank you for your help, and if there is a way, to implement tempalte functions in the cpp file please contact me! – Marcell Apr 19 '12 at 20:31
  • @Marcell read the link in this answer. It describes what you seem to want to do. You can **specialize the template function** in your cpp file, and the linker can use that specialization. – tmpearce Apr 19 '12 at 20:39
  • The specialization is something else. I just want a template, that accept every class. I don't care with specializations. – Marcell Apr 19 '12 at 21:03