Simple instantiating of class templates

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;
}
 
M

Moonlit

Hi

How is the compiler supposed to know what your constructor does if you
include the header in another module. i.e. I it replaces T with the actual
type. The compiler usually isn't smart enough to try to find the C++ source
file of the template and replace the template with your actual type..

In other words move everything from the template CPP file into the template
header file.


Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
A

asterixgallier

Ohh thank you very much for your fast reply.

This is although an explanation why i haven't found an example in this
way on the internet =)

Thank you
Hi

How is the compiler supposed to know what your constructor does if you
include the header in another module. i.e. I it replaces T with the actual
type. The compiler usually isn't smart enough to try to find the C++ source
file of the template and replace the template with your actual type..

In other words move everything from the template CPP file into the template
header file.


Regards, Ron AF Greve

http://moonlit.xs4all.nl

asterixgallier said:
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;
}
 
R

red floyd

asterixgallier said:
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?

Yeah, http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top