Connecting Tech Pros Worldwide Help | Site Map

Error C2768 Illegal use of explicit template arguments

Newbie
 
Join Date: Apr 2007
Posts: 1
#1: Apr 22 '07
The following error arises when I try to compile [url=http://sourceforge.net/projects/tinybind/]tinybind, an apparently unsupported code project. (A compile-killing bug reported a year ago has gone unanswered.) So I'm looking for help outside.

Apparently VC2003 introduced this error, C2768, and VC2005 made matters worse. Anyway, here's the template definition:
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. TiXmlBinding<T> const *
  3. GetTiXmlBinding( T const &, IdentityBase  );
  4.  
Here's a target class:
Expand|Select|Wrap|Line Numbers
  1. struct MyData
  2. {
  3.   int i;
  4.   double d;
  5.   char const * s;
  6.   std::vector<int> vec;
  7.   int iref;
  8.  
  9.   void setIntvalue( int in ) {
  10.     i = in;
  11.   }
  12.   int intvalue() {
  13.     return i;
  14.   }
  15.  
  16.   int & getIRef() {
  17.     return iref;
  18.   }
  19. };
  20.  
And here's the template instantiation that gets the C2768 error, just before the function body:
Expand|Select|Wrap|Line Numbers
  1. TiXmlBinding<MyData> const *
  2. GetTiXmlBinding<MyData>( MyData const &, Identity<MyData> )
  3. {
  4.   static MemberTiXmlBinding<MyData> binding;
  5.   if( binding.empty() ) {
  6.     binding.AddMember( "ITAG", Member(&MyData::i) );
  7.     binding.AddMember( "ITAGGETSET", Member(&MyData::intvalue, &MyData::setIntvalue) );
  8.     binding.AddMember( "DTAG", Member(&MyData::d) );
  9.     binding.AddMember( "STAG", Member(&MyData::s) );
  10.     binding.AddMember( "VEC", Member(&MyData::vec) );
  11.     binding.AddMember( "IREF", Member(&MyData::getIRef) );
  12.   }
  13.   return &binding;
  14. }
Anybody understand the complaint and how it might be mended? Microsoft's doc says put "template <>" in front of the function definition; that helps in some cases but not this one.

(I'm not showing you the lengthy MemberTiXmlBinding class, because it seems pretty clear this is an issue of the function header, not its body. Right?)
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Apr 22 '07

re: Error C2768 Illegal use of explicit template arguments


This code:
Quote:
template<class T>
TiXmlBinding<T> const *
GetTiXmlBinding( T const &, IdentityBase );
looks odd.

Like where's the function??

You do not use function prototypes wioth templates. The template is like a macro. The compiler makes a copy of it (called a specialization), changes the placeholders (T) to your type and then calls the function.

I expected something like:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. TiXmlBinding<T> const *
  3. GetTiXmlBinding( T const &, IdentityBase  )
  4. {
  5.     //TODO: Put function body here
  6. }
  7.  
Reply