At 20:03 2007-02-13, Irwan Setiawan wrote:
Dear All,
Please help me to fix template problem. Here is the problem:
if i compiled, i got 4 error messages and one of them is like this (
fyi : i use ms visual c+)
test.obj : error LNK2001: unresolved external symbol "public:
__thiscall CExcept<enum ENUM_EXCEPTION>::CExcept<enum
ENUM_EXCEPTION>(enum ENUM_EXCEPTION)"
(<mailto:[EMAIL PROTECTED]@@@@[EMAIL PROTECTED]@@@Z>[EMAIL PROTECTED]@@@@[EMAIL PROTECTED]@@@Z)
but if i dropped the file MyExcept.cpp and use inline function in
MyExcept.h the errors will solve.
yup, that's how templates work on most compilers.... the compiler has
to be able to "see" the template code when you use it.
Thanks a lot
// FILE : MyExcept.h
enum ENUM_EXCEPTION
{
ENUM_1 = 1,
ENUM_2,
ENUM_3
};
template<class C> class CExcept
{
public:
CExcept( const C err ); // { m_err = err; }
CExcept(CExcept& rhs); // { m_err = rhs.m_err; }
C GetError() const; // { return m_err; }
CExcept& operator()(C rhs); // { m_err = rhs; return *this; }
public:
C m_err;
};
// FILE : MyExcept.cpp
#include "myexcept.h"
template<class C> CExcept<C>::CExcept( const C err ) { m_err = err; }
template<class C> CExcept<C>::CExcept( CExcept<C>& rhs ) { m_err =
rhs.m_err; }
template<class C> C CExcept<C>::GetError() const { return m_err; }
template<class C> CExcept<C>& CExcept<C>::operator()( C rhs )
{
m_err = err;
return *this;
}
// FILE : test.cpp
#include <iostream>
#include "myexcept.h"
CExcept<ENUM_EXCEPTION> ExceptTest(ENUM_1);
void AlwaysException() { throw ExceptTest(ENUM_2); }
int main( int argc, char* argv[] )
{
try { AlwaysException(); }
catch( ... )
{
std::cout << "Return exception : " << ExceptTest.GetError() << std::endl;
}
std::cout << "end of program!" << std::endl;
return 0;
}
Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
"There oughta be a law"