A
Ajay Daptardar
Hi,
I have the following problem. Consider this:
// codec.h
template <class T>
class codec {
public:
codec(T val);
private:
T val;
};
// codec.cpp
#include "codec.h"
template <class T>
codec<T>::codec(T val) { this->val = val; }
// main.cpp
#include "codec.h"
void main() { codec<int> cc(7); }
I compile using: c++ codec.cpp main.cpp
and I get:
/tmp/ccwlkOaV.o: In function `main':
/tmp/ccwlkOaV.o(.text+0x10): undefined reference to `codec<int>::codec(int)'
collect2: ld returned 1 exit status
But if I either move the contents of codec.cpp to the codec.h file OR
move the main to codec.cpp, everything works out. Why ?
Is there a way to make this work having the three files that I have ?
Thanks -
I have the following problem. Consider this:
// codec.h
template <class T>
class codec {
public:
codec(T val);
private:
T val;
};
// codec.cpp
#include "codec.h"
template <class T>
codec<T>::codec(T val) { this->val = val; }
// main.cpp
#include "codec.h"
void main() { codec<int> cc(7); }
I compile using: c++ codec.cpp main.cpp
and I get:
/tmp/ccwlkOaV.o: In function `main':
/tmp/ccwlkOaV.o(.text+0x10): undefined reference to `codec<int>::codec(int)'
collect2: ld returned 1 exit status
But if I either move the contents of codec.cpp to the codec.h file OR
move the main to codec.cpp, everything works out. Why ?
Is there a way to make this work having the three files that I have ?
Thanks -