Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

Template classes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 15 March 2012 - 07:00 AM

Hi,

I made a template class like this:

template<class identifier>
class myclass
{
public:
   void function();
};

and I want to define the function in a cpp file but I cant figure out how to do this. I tried this:

template<class identifier>
myclass::function()
{
   //---
}

but I get the error - use of template requires template argument list.

Is there any way I can define the functions for the class in a cpp file or would I just need to do it in the header?

thanks,

Sponsor:

#2 SuperVGA   Members   -  Reputation: 879

Like
1Likes
Like

Posted 15 March 2012 - 07:06 AM

Hi Luke,

Templates need to be explicitly defined.
- You need to implement the template function in the header like you suggested.

#3 Kraecker   Members   -  Reputation: 208

Like
1Likes
Like

Posted 15 March 2012 - 07:37 AM

Hi there,

as SuperVGA said, you have to implement your methods / functions in the header file.

BUT you can include other files in those headers.

So you write your declaration in your header file and include the ".inl" file where your actual implementation is.

-- Edit --

Maybe take a look at http://stackoverflow.com/questions/1208028/significance-of-a-inl-file-in-c

#4 SiCrane   Moderators   -  Reputation: 6768

Like
1Likes
Like

Posted 15 March 2012 - 07:48 AM

To fix the compiler error you would change your code to:

template<class identifier>
void myclass<identifier>::function()
{
   //---
}
As noted above you'll get a linker error without explicit instantiation if you put that code in a source file.

#5 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 15 March 2012 - 01:58 PM

Ah ok thanks!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS