1

I have this file: myfuncts.hpp:

#ifndef FUNCTS_
#define FUNCTS_
namespace root {
namespace functs {
template <typename T>
class f1 {
public:
  T r;
  f1();
};
}
}
#endif

I have the implementation: myfuncts.cpp:

#include "myfuncts.hpp"
template <typename T>
root::functs::f1<T>::f1() { /* do somethng */ }

Then I have my main routine:

#include "impulses.hpp"
int main(int argc, char** argv);
int main(it argc, char** argv) {
  root::functs::f1<double> f();
}

I compile it:

g++ main.cpp functs.cpp

And got this:

/tmp/ccrdJEQt.o: In function main': main.cpp:(.text+0x53): undefined reference toroot::functs::f1::f1()' collect2: ld returned 1 exit status

What am I doing wrong?

Andry
  • 16,172
  • 27
  • 138
  • 246

1 Answers1

6

You might want to read about the most vexing parse, because when you do

root::functs::f1<double> f();

you declare f to be a function which returns a root::functs::f1<double> object.

You might also want to read this old question, as to why you actually get the undefined reference error. It's because the header file doesn't fully define the class. For templated classes, the complete implementation have to be in the header file as well.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • How can I be so stupid... you're right!!! I remember my god... Thanks a lot, of course the header gotta go there... – Andry Sep 03 '13 at 07:38