A
asterixgallier
Hello at all,
i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template
this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.
Have got anyone an idea?
Here is my code:
-------------------------------
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
template <class T>
class MyClass {
public:
MyClass(T MyVal);
void MyFunction(void);
private:
T Val;
};
#endif // MYCLASS_H
MyClass.cpp
#include "MyClass.h"
#include <iostream>
template <class T>
MyClass<T>::MyClass(T MyVal) {
this->Val = MyVal;
}
template <class T>
void MyClass<T>::MyFunction() {
std::cout << "MyFunction";
}
MyTest.cpp
#include "MyClass.h"
int main(int argc, char* argv[]) {
MyClass<int> var(1);
return 0;
}
i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template
this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.
Have got anyone an idea?
Here is my code:
-------------------------------
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
template <class T>
class MyClass {
public:
MyClass(T MyVal);
void MyFunction(void);
private:
T Val;
};
#endif // MYCLASS_H
MyClass.cpp
#include "MyClass.h"
#include <iostream>
template <class T>
MyClass<T>::MyClass(T MyVal) {
this->Val = MyVal;
}
template <class T>
void MyClass<T>::MyFunction() {
std::cout << "MyFunction";
}
MyTest.cpp
#include "MyClass.h"
int main(int argc, char* argv[]) {
MyClass<int> var(1);
return 0;
}