473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template declaration and definition

Hello,

I should separate the definition and declaration of template code. This
works fine for non-specialized templates. But I do not know how to do
this for specialized templates.

Example:

template<typename T>
class C_B
{
public:
T var;
public:
C_B( void );
virtual ~C_B( void );
};

template<typename T>
C_B<T>::C_B( void ) : var(0){ return;}

template<typename T>
C_B<T>::~C_B( void ) { return;}
// specialized for char*
template<>
class C_B<char*>
{
public:
static const int MAXCHARS = 50;
char* var;
public:
C_B( void );
virtual ~C_B( void );
};

// this won't compile
template<>
C_B<char*>::C_B( void )
: var( new char[MAXCHARS+1] )
{
var[0]='\0';
return;
}
Jun 27 '08 #1
5 1713
ruebezaehler wrote:
I should separate the definition and declaration of template code. This
works fine for non-specialized templates. But I do not know how to do
this for specialized templates.

Example:

template<typename T>
class C_B
{
public:
T var;
public:
C_B( void );
virtual ~C_B( void );
Please, please, drop the habit of putting something between parentheses
where nothing is _meant_ to be there. It's C-ism, it's ugly.
};

template<typename T>
C_B<T>::C_B( void ) : var(0){ return;}

template<typename T>
C_B<T>::~C_B( void ) { return;}
// specialized for char*
template<>
class C_B<char*>
{
public:
static const int MAXCHARS = 50;
char* var;
public:
C_B( void );
virtual ~C_B( void );
};

// this won't compile
"Won't compile" - what does it mean? What error message do you get?
template<>
C_B<char*>::C_B( void )
: var( new char[MAXCHARS+1] )
{
var[0]='\0';
return;
}
I think you're mixing two concepts. The 'template<>' syntax (verbatim)
starts a declaration/definition of a full specialisation of a template.
You already got that when you fully specialised class C_B template.
Now, when you deed to define the already specialised constructor from
that class, you should omit the 'template<>' construct and simply write

C_B<char*>::C_B() ...

Try it, let us know how it turns out.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
Please, please, drop the habit of putting something between parentheses
where nothing is _meant_ to be there. It's C-ism, it's ugly.
Please, please, drop the habit ...: I need 3 pleases to do that.
Jun 27 '08 #3
Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
ruebezaehler wrote:
>I should separate the definition and declaration of template code. This
works fine for non-specialized templates. But I do not know how to do
this for specialized templates.

Example:

template<typename T>
class C_B
{
public:
T var;
public:
C_B( void );
virtual ~C_B( void );

Please, please, drop the habit of putting something between parentheses
where nothing is _meant_ to be there. It's C-ism, it's ugly.
>};

template<typename T>
C_B<T>::C_B( void ) : var(0){ return;}

template<typename T>
C_B<T>::~C_B( void ) { return;}
// specialized for char*
template<>
class C_B<char*>
{
public:
static const int MAXCHARS = 50;
char* var;
public:
C_B( void );
virtual ~C_B( void );
};

// this won't compile

"Won't compile" - what does it mean? What error message do you get?
>template<>
C_B<char*>::C_B( void )
: var( new char[MAXCHARS+1] )
{
var[0]='\0';
return;
}

I think you're mixing two concepts. The 'template<>' syntax (verbatim)
starts a declaration/definition of a full specialisation of a template.
You already got that when you fully specialised class C_B template. Now,
when you deed to define the already specialised constructor from that
class, you should omit the 'template<>' construct and simply write

C_B<char*>::C_B() ...

Try it, let us know how it turns out.

V
undefined reference to `vtable for C_B<char*>'
Jun 27 '08 #4
On May 23, 12:00 pm, ruebezaehler <ruebezaeh...@web.dewrote:
Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
C_B<char*>::C_B() ...
Try it, let us know how it turns out.
V

undefined reference to `vtable for C_B<char*>'
You need to define the specialized ~C_B's destructor as well, just as
you need to define any class's destructor or other members if you
declare them. Explicitly specializing the entire class is effectively
creating a new, completely independent class. You don't get any of the
stuff from the default template.

Jason
Jun 27 '08 #5
Am Fri, 23 May 2008 09:19:12 -0700 schrieb ja************@gmail.com:
On May 23, 12:00 pm, ruebezaehler <ruebezaeh...@web.dewrote:
>Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
C_B<char*>::C_B() ...
Try it, let us know how it turns out.
V

undefined reference to `vtable for C_B<char*>'

You need to define the specialized ~C_B's destructor as well, just as
you need to define any class's destructor or other members if you
declare them. Explicitly specializing the entire class is effectively
creating a new, completely independent class. You don't get any of the
stuff from the default template.

Jason
Hallo Jason,

thank You for that hint. I overlooked the forgotten definition of the ~
and =.

It works.

best regards, ruebezaehler
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
10
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
2
by: Alan | last post by:
Does a template class declaration, like template <class T> have to come immediately prior to the declaration of the function, e.g., T do_something (T something) { . . . } that uses it?
5
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
1
by: ctoo | last post by:
The following compiles and works with g++ 3.4.4 and Borland C++ Builder 6 update#4: #include <iostream> #include <vector> #include <utility> // declaration and definition for primary class...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.