Sign in to follow this  

Template specialization error

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

I have a template class, and I try that for a certain class (T=Scope) the template will behave differently. So I did:
template <class T>;
class ReferencePointer {
};

template <>
class ReferencePointer<Scope> {
};
But I get the errors: C2908: explicit specialization; 'ReferencePointer<T>' has already instantiated with [ T=Scope ] C2766: explicit specialization; 'ReferencePointer<Scope>' has already been defined What am I doing wrong? [Edited by - The C modest god on October 2, 2006 9:43:05 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Dranith
You declared the first template incorrectly. Try this:

template <class T>
class ReferencePointer {
};

template <>
class ReferencePointer<Scope> {
};


Sorry, you are right.
I just wrote it wrong, my original code didnt had this problem.
So the error I get is also valid for this too.

Share this post


Link to post
Share on other sites
Well, my next guess would be that you are including the header that defines this template in multiple files, but you forgot to add a header guard. This is usually the problem when you get those "already defined" errors when dealing with classes defined in headers.

If it isn't that, then I have no clue without seeing more of the code.

Share this post


Link to post
Share on other sites
Quote:
Original post by omgomghilol
I don't think you can actually specialize a class like that; class specialization must be done by specializing the member functions IIRC.


Both are possible, though mutually exclusive.

Share this post


Link to post
Share on other sites
The following code compiles okay for me.

class Scope
{
};

template <class T>
class ReferencePointer {
};

template <>
class ReferencePointer<Scope> {
};

main()
{
ReferencePointer<Scope> p;
}



This is the way you have to specialize template classes in C++.

Your error is not in the code you posted. The error is in the code you did not post. Check your include guards.

Share this post


Link to post
Share on other sites

This topic is 4089 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this