C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template syntax question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
John Dumais
Guest





PostPosted: Sat Jul 30, 2005 12:32 am    Post subject: template syntax question Reply with quote



Hello,

I have been trying to develop a realtively simple policy template class
and ran into a situation where I think I don't understand template
syntax when the class function definitions are in a .cpp file. As an
example, this compiles, links and runs...

---------------------------------------------------------

Policy.h:

#if !defined(POLICY_INCLUDED)
#define POLICY_INCLUDED

#include <string>

struct LoggingBase
{
virtual ~LoggingBase() {}

virtual void SetLogName(const std::string &logName) = 0;
virtual void Append(const std::string &msg) = 0;
};

struct NoLogging : public LoggingBase
{
virtual void SetLogName(const std::string &logName) {}
virtual void Append(const std::string &msg) {}
};

class FileLogging : public LoggingBase
{
std::string m_logName;

public:
virtual ~FileLogging() {}

virtual void SetLogName(const std::string &logName);
virtual void Append(const std::string &msg);
};

template <typename LogPolicy>
class HasPolicy
{
LogPolicy l;

public:
HasPolicy() { l.SetLogName("logName"); } // <--- implementation

void DoSomething() { l.Append("msg"); } // <--- implementation
};

#endif

---------------------------------------------------------

PolicyImpl.cpp:

#include "Policy.h"

void FileLogging::SetLogName(const std::string &logName)
{
m_logName = logName;
}

void FileLogging::Append(const std::string & msg)
{
// do something here
}

---------------------------------------------------------

TestProgram.cpp:

#include "Policy.h"

int main(int argc, char *argv[])
{
HasPolicy p.DoSomething();

HasPolicy<NoLogging> n;
n.DoSomething();

return 0;
}

The problem comes in when I try to move the implementation of class
functions in the HasPolicy class to a .cpp file. If I put the following
in the PolicyImpl.cpp file, the application compiles but produces
unresolved externals of the form...

HasPolicy<class FileLogging>::HasPolicy<class FileLogging>(void)
HasPolicy<struct NoLogging>::HasPolicy<struct NoLogging>(void)
void HasPolicy<class FileLogging>::DoSomething(void)
void HasPolicy<struct NoLogging>::DoSomething(void)

What I put in PolicyImpl.cpp that causes the problem is this...

template <typename LogPolicy>
HasPolicy<LogPolicy>::HasPolicy()
{
l.SetLogName("foo");
}

template <typename LogPolicy>
void HasPolicy<LogPolicy>::DoSomething()
{
l.Append("foo");
}

What I'm doing appears to be wrong, but I can't hit upon the correct syntax.

Any suggestions are most appreciated.

----------------

If you would like to reply directly through e-mail, please remove the
"nospam_" preamble to my return address.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
lampard
Guest





PostPosted: Sat Jul 30, 2005 10:11 am    Post subject: Re: template syntax question Reply with quote



For templates the implementation should be in the header file rather
than cpp file that is what I do regularly.
Why should it be in header file I do not have any good answer but wish
some one will provide.
I think the compiler need to know this information to expand the
templates for the class usage in your case LogPolicy.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bo Persson
Guest





PostPosted: Sat Jul 30, 2005 11:51 am    Post subject: Re: template syntax question Reply with quote




"lampard" <devendermarri (AT) hotmail (DOT) com> skrev i meddelandet
news:1122694395.113117.239120 (AT) g47g2000cwa (DOT) googlegroups.com...
Quote:
For templates the implementation should be in the header file rather
than cpp file that is what I do regularly.
Why should it be in header file I do not have any good answer but wish
some one will provide.
I think the compiler need to know this information to expand the
templates for the class usage in your case LogPolicy.

The short answer is that this is needed for compilers not implementing
the 'export' keyword. Otherwise it would work well to declare the
templates in the header and implement them somewhere else.


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Mon Aug 01, 2005 9:39 am    Post subject: Re: template syntax question Reply with quote

John Dumais wrote:
Quote:
I have been trying to develop a realtively simple policy template class
and ran into a situation where I think I don't understand template
syntax when the class function definitions are in a .cpp file.

Take a look at the C++ FAQ at parashift.com.

Uli

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.