C++ template

Started by
24 comments, last by Zahlman 15 years, 6 months ago
example.h
template <class X, class Y>
class Example{
	public:
    Example(X m, Y n);
    ~Example();
	public:
		X x;
		Y y;
};
example.cpp
template <class X, class Y>
Example<X, Y>::Example(X m, Y n): x(m), y(n){

}

template <class X, class Y>
Example<X, Y>::~Example(){

}
main()
int main(int argc, char *argv[]){
  Example<int, char> a(2, 'y');
  cout<<a.x<<"\t"<<a.y<<endl;

  return EXIT_SUCCESS;
}

Whats wrong with the above Code ?? Its giving me Compilation errors.
Advertisement
Which errors are you getting, and on which lines?

Edit:

Looks like the problem is that your template member functions aren't visible when the template gets instantiated.

You can't implement the constructor and destructor (or any other member function of a class template) in a separate .cpp file.
It compiles and runs fine for me. What errors are you getting?

<edit> Spoonbender is correct, put the definitions in your header file.
In the future you should post what your errors are, not just that you are getting errors.

Unless your compiler supports the export keyword, and I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)
Sorry I forgot to post the error message.

These are the error message
undefined reference to `Example<int, char>::Example(int, char)'undefined reference to `Example<int, char>::~Example()'undefined reference to `Example<int, char>::~Example()'


I dont think(like) Its good to code them in the header file.

So Whats the solution if I want to keep them in the separate cpp file.

EDIT

I am using gcc and as far I know it supports export keyword.
I already told you your options: use explicit instantiation or use a compiler that supports the export keyword.
Quote:Original post by nlbs
I am using gcc and as far I know it supports export keyword.

It doesn't.
Quote:Original post by SiCrane
I already told you your options: use explicit instantiation or use a compiler that supports the export keyword.


Doesn't gcc support export keyword ?? EDIT: sorry already answered

Can you give some explanation about explicit instantiation ??
Quote:Original post by nlbs
Can you give some explanation about explicit instantiation ??


Just include the definitions in the header file. This is how the C++ standard library does it; there is no other choice.
Quote:Original post by Simian Man
Quote:Original post by nlbs
Can you give some explanation about explicit instantiation ??


Just include the definitions in the header file. This is how the C++ standard library does it; there is no other choice.


Sorry I didn't understand correctly.
Hmm did you mean put the constructor in the header file ??

This topic is closed to new replies.

Advertisement