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>::operator<(const Message& x) const throw()
{
return priority_ < x.priority_;
}
----- end of message.cpp ---------- 4 4238
"Me" <al***********@rogers.com> wrote in message
news:56******************@news04.bloor.is.net.cabl 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>::priority_type Message<M,P>::getPriority() const
throw()
{
return priority_;
}
You need to use typename so that the compiler can tell that
Message<M,P>::priority_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
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bi************@ID-196037.news.uni-berlin.de... 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. 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". (:
I was trying to use the "export" keyword, but I just found out that my
compiler doesn't support it yet!
Thanks, Me.
"Me" <al***********@rogers.com> wrote in message
news:Cv*****************@news04.bloor.is.net.cable .rogers.com... "John Harrison" <jo*************@hotmail.com> wrote in message news:bi************@ID-196037.news.uni-berlin.de... 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.
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". (:
"Me" <al***********@rogers.com> wrote in message
news:Sw******************@news04.bloor.is.net.cabl 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: nikola |
last post by:
Hi all,
I was working with a simple function template to find the min of two values.
But since I would like the two values to be different (type) I dont know
what kind of value (type) it will...
|
by: Chris F Clark |
last post by:
In our C++ project we have some internal bug reporting macros that we
use to get useful information when the program does something
unexpected. Essentially at the point of the error, we invoke an...
|
by: Adam Dziendziel |
last post by:
Hi all!
I'm writing a luabind/boost::python-like binding utility for a Squirrel
language to generating wrapper-functions for C++ classes and have a
problem with passing the pointer-to-member...
|
by: Chris |
last post by:
I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.
...
|
by: Raf256 |
last post by:
I have base template class A<B>, and son class B.
Inside A<B> constrcutor, can I access a pointer to B, from "this"?
like
A<B>::A() : pointerToB(static_cast<B*>this) { }
I will use the...
|
by: Christof Warlich |
last post by:
Hi,
consider this:
template<typename Tclass X {
public:
void doSomething(T t);
};
int main(void) {
X<intx;
|
by: n.torrey.pines |
last post by:
I have the following tree definition:
template<typename T>
struct tree {
T first;
vector<tree<T second; // branches
};
which compiles successfully. What I'd like to do though is to use...
|
by: Peter Olcott |
last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a
I think that the operator() member function does not work correctly, does
anyone else know how to make a template for making two...
|
by: David O |
last post by:
I am using the CRTP (Curiously Recurring Template Pattern) to provide a
set of low-level functions customizable by environment. Using CRTP
ensures that all the function have the same signatures,...
|
by: abir |
last post by:
i have a template as shown
template<typename Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typename T,typename Aclass...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |