Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Template proble...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 9 Topic 45795 of 45898
Post > Topic >>

Template problems

by Tim Frink <plfriko@[EMAIL PROTECTED] > May 7, 2008 at 04:04 PM

Hi,

I want to use a callback function together with 
templates. Let's say I've this code:

File a.h:

class A
{
   private:
     template< typename T >
     void func( bool( T::*f)(void) );
};
-----------------

File a.cc:

template< typename T >
void A::func( bool( T::*f)(void) )
{
  f();
}
------------------

and another class B that uses "func":

File b.cc:
#include "a.h"

bool B::func2(void)
{
   return true;
}

void B::func3()
{
  A a;
  a->func( &B::func2 );
}

During compilation I get the error
undefined reference to `void A::func<B>(bool (B::*)())'.

The problems seem to be that during compilation of class B, the 
template function A::func is not completely known (just the
function declaration of the header file, but not the function
definition). I assume that one solution is to move the function
body of A::func into the header file a.h to make it visible during
compilation of class B. 

However, this seems not to be the best solution because I must
reveal the implementation of A::func to anyone who wants to use
it. Without templates, I could compile class A into a library
and just provide the header file a.h together with the library.
So, the users would not see the implementation of class A. 
However, when I move code from the source file into the header
file, I show my implementation which I would like to avoid.
Are there any solutions for that? Or are templates in general
crucial when source code must be kept closed?

Best regards,
Tim




 9 Posts in Topic:
Template problems
Tim Frink <plfriko@[EM  2008-05-07 16:04:49 
Re: Template problems
"sk_usenet" <  2008-05-07 09:35:09 
Re: Template problems
Puppet_Sock <puppet_so  2008-05-07 09:39:05 
Re: Template problems
"Victor Bazarov"  2008-05-07 12:58:24 
Re: Template problems
Stephan Ceram <linuxka  2008-05-07 17:28:44 
Re: Template problems
"Victor Bazarov"  2008-05-07 13:41:35 
Re: Template problems
Joe Greer <jgreer@[EMA  2008-05-07 21:08:27 
Re: Template problems
Tim Frink <plfriko@[EM  2008-05-07 21:51:07 
Re: Template problems
Joe Greer <jgreer@[EMA  2008-05-08 20:17:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue May 13 14:55:23 CDT 2008.