Jump to content
  • Advertisement
Sign in to follow this  
lukesmith123

Template classes

This topic is 2405 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

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,

Share this post


Link to post
Share on other sites
Advertisement
Hi Luke,

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

Share this post


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

Share this post


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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!