• Advertisement
Sign in to follow this  

C++ template

This topic is 3478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
It compiles and runs fine for me. What errors are you getting?

<edit> Spoonbender is correct, put the definitions in your header file.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I already told you your options: use explicit instantiation or use a compiler that supports the export keyword.

Share this post


Link to post
Share on other sites
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 ??

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ??

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement