Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template implementation problem.

Reply
Thread Tools

Template implementation problem.

 
 
Me
Guest
Posts: n/a
 
      08-30-2003
I am not understanding an aspect of how to implement a class template in a
..cpp file.

What I do know is that there are at least two problems with my understanding
of how to accomplish the definition of a template outside the class. One
problem is how to refer to a typedef that is a member of the classe's public
interface in the .cpp file. The second problem is that if I modify my code
to use the template variables and not my typedefs, then it will compilem,
but there will be linker errors. When I used the typedefs in my .cpp file
the result was that the compiler thought I was defining a function template.

I would prefer to use my typdefs instead of the template argument names. For
instance,
typedef P priority_type;

Perhaps it is my use of default template arguments as well?

Below are the two files.

-------- message.h ----------
#ifndef MESSAGE_INC
#define MESSAGE_INC

#include <string>
using std::string;

template<class M = string, class P = int>
class Message {
public:
typedef M message_type;
typedef P priority_type;

explicit Message(const message_type& m, const priority_type& p = 1)
throw()
:message_(m), priority_(p) {}

priority_type getPriority() const throw();
message_type getMessage() const throw();
bool operator<(const Message& x) const throw();

private:
priority_type priority_;
message_type message_;
};

#endif

----- end of message.h ----------

----- message.cpp ----------
#include "message.h"

template<class M, class P>
inline
// I would like to use the "priority_type" typedef and not P, but it won't
work.
// Message<M,P>:riority_type Message<M,P>::getPriority() const throw()
P Message<M,P>::getPriority() const throw()
{
return priority_;
}

template<class M, class P>
inline
M Message<M,P>::getMessage() const throw()
{
return message_;
}

template<class M, class P>
inline
bool Message<M,P>:perator<(const Message& x) const throw()
{
return priority_ < x.priority_;
}

----- end of message.cpp ----------


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-30-2003

"Me" <> wrote in message
news:56%3b.99645$_ e.rogers.com...
> I am not understanding an aspect of how to implement a class template in a
> .cpp file.
>
> What I do know is that there are at least two problems with my

understanding
> of how to accomplish the definition of a template outside the class. One
> problem is how to refer to a typedef that is a member of the classe's

public
> interface in the .cpp file.


This compiles

typename Message<M,P>:riority_type Message<M,P>::getPriority() const
throw()
{
return priority_;
}

You need to use typename so that the compiler can tell that
Message<M,P>:riority_type is a type and not something else.

> The second problem is that if I modify my code
> to use the template variables and not my typedefs, then it will compilem,
> but there will be linker errors.


That's because your template code should be in header files only, all of
it. The entire template definition must be available to the compiler when
the template is used. The linker will not resolve templates. The easiest way
to achieve this is to put all template code in header files.

This question is covered in the FAQ

http://www.parashift.com/c++-faq-lit...html#faq-34.12

john


 
Reply With Quote
 
 
 
 
Me
Guest
Posts: n/a
 
      08-30-2003

"John Harrison" <> wrote in message
news:bipvvi$bs6bt$...
> This compiles
>
> typename Message<M,P>:riority_type Message<M,P>::getPriority() const
> throw()
> {
> return priority_;
> }


Ah, that makes so much sense.

> That's because your template code should be in header files only, all of
> it. The entire template definition must be available to the compiler when
> the template is used. The linker will not resolve templates. The easiest

way
> to achieve this is to put all template code in header files.


I read the portion of The C++ Programming language that covers that. I
can't believe I didn't recall it.

>
> This question is covered in the FAQ
>
>

http://www.parashift.com/c++-faq-lit...html#faq-34.12
>
> john
>
>


Thank you so much for your help!, Me.

And thanks for not writing "RTFM". (:


 
Reply With Quote
 
Me
Guest
Posts: n/a
 
      08-30-2003
I was trying to use the "export" keyword, but I just found out that my
compiler doesn't support it yet!

Thanks, Me.

"Me" <> wrote in message
news:Cv%3b.99825$_ .rogers.com...
>
> "John Harrison" <> wrote in message
> news:bipvvi$bs6bt$...
> > This compiles
> >
> > typename Message<M,P>:riority_type Message<M,P>::getPriority() const
> > throw()
> > {
> > return priority_;
> > }

>
> Ah, that makes so much sense.
>
> > That's because your template code should be in header files only, all

of
> > it. The entire template definition must be available to the compiler

when
> > the template is used. The linker will not resolve templates. The easiest

> way
> > to achieve this is to put all template code in header files.

>
> I read the portion of The C++ Programming language that covers that. I
> can't believe I didn't recall it.
>
> >
> > This question is covered in the FAQ
> >
> >

>

http://www.parashift.com/c++-faq-lit...html#faq-34.12
> >
> > john
> >
> >

>
> Thank you so much for your help!, Me.
>
> And thanks for not writing "RTFM". (:
>
>



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-30-2003

"Me" <> wrote in message
news:Sw%3b.99835$_ e.rogers.com...
> I was trying to use the "export" keyword, but I just found out that my
> compiler doesn't support it yet!
>
> Thanks, Me.
>


You might be waiting a long time. Comeau C++ supports the export keyword.

http://www.comeaucomputing.com/

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
hiding non template dependent implementation in a template class er C++ 1 10-16-2007 01:15 AM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57