Template specialization error

Started by
5 comments, last by Bregma 17 years, 6 months ago
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]
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
You declared the first template incorrectly. Try this:

template <class T>
class ReferencePointer {
};

template <>
class ReferencePointer<Scope> {
};
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.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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.
I don't think you can actually specialize a class like that; class specialization must be done by specializing the member functions IIRC.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement