Template implementation problem.

M

Me

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>::priority_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>::eek:perator<(const Message& x) const throw()
{
return priority_ < x.priority_;
}

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

John Harrison

Me said:
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>::priority_type Message<M,P>::getPriority() const
throw()
{
return priority_;
}

You need to use typename so that the compiler can tell that
Message said:
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-lite/containers-and-templates.html#faq-34.12

john
 
M

Me

John Harrison said:
This compiles

typename Message<M,P>::priority_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.

Thank you so much for your help!, Me.

And thanks for not writing "RTFM". :)
 
M

Me

I was trying to use the "export" keyword, but I just found out that my
compiler doesn't support it yet!

Thanks, Me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top