| View previous topic :: View next topic |
| Author |
Message |
John Dumais Guest
|
Posted: Sat Jul 30, 2005 12:32 am Post subject: template syntax question |
|
|
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
|
Posted: Sat Jul 30, 2005 10:11 am Post subject: Re: template syntax question |
|
|
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
|
Posted: Sat Jul 30, 2005 11:51 am Post subject: Re: template syntax question |
|
|
"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
|
Posted: Mon Aug 01, 2005 9:39 am Post subject: Re: template syntax question |
|
|
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 |
|
 |
|