Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template specialization - Error LNK2005?

369 views
Skip to first unread message

andru123

unread,
Feb 11, 2003, 8:20:26 AM2/11/03
to
I have created template in some.h file. This some.h file is imported by a
LOT of other files (all of them actually).

Then, I decided to specialize some of the template class methods. I did it
in some.h file, of course, where the template itself is located.

When I compile, it gives me linker error for every file that imports some.h,
saying that these specialized methods already defined in xxx.obj (the first
file to cmpile, which includes some.h).

Are there some specific rules about that that I might be missing?

Below is the code for template


// template to set temporally some Acad global variables

template <typename T> class TemporarilySet
{
public:
TemporarilySet(const CString acadGlobalVarName,
const T tempValue)
{
AcadGlobalVarName = acadGlobalVarName;
OldValue = getVar();
setVar(tempValue);
}

~TemporarilySet()
{
setVar(OldValue);
}

protected:
int setVar(T value);
T getVar();

private:
CString AcadGlobalVarName;
T OldValue;
};

// template specializations

template <> int
TemporarilySet<int>::getVar()
{
return log_GetIntVar(AcadGlobalVarName);
}

template <> int
TemporarilySet<int>::setVar(int value)
{
return log_SetIntVar(AcadGlobalVarName, value);
}

template <> CString
TemporarilySet<CString>::getVar()
{
return log_GetStringVar(AcadGlobalVarName);
}

template <> int
TemporarilySet<CString>::setVar(CString value)
{
return log_SetStringVar(AcadGlobalVarName, value);
}

ramesh

unread,
Feb 11, 2003, 8:30:04 AM2/11/03
to
use #ifndef #endif directives.

eg:
#ifndef ME
#define ME


your code follows here...


#endif
Ramesh

"andru123" <andru123 at hotmail dot com> wrote in message
news:eG2fLBd0CHA.2184@TK2MSFTNGP09...

andru123

unread,
Feb 11, 2003, 10:09:12 AM2/11/03
to
Is it a crack or official recomendation?

"ramesh" <k...@srasys.co.in> schreef in bericht
news:#VxsfFd0CHA.2204@TK2MSFTNGP09...

Craig Powers

unread,
Feb 12, 2003, 11:06:15 AM2/12/03
to
andru123 wrote:
>
> I have created template in some.h file. This some.h file is imported by a
> LOT of other files (all of them actually).
>
> Then, I decided to specialize some of the template class methods. I did it
> in some.h file, of course, where the template itself is located.
>
> When I compile, it gives me linker error for every file that imports some.h,
> saying that these specialized methods already defined in xxx.obj (the first
> file to cmpile, which includes some.h).
>
> Are there some specific rules about that that I might be missing?

They need to be either identified as inline or moved to a single cpp
file.

Otherwise, the compiler generates the specializations in each cpp file
that includes the header file, which is no different from multiple
definitions of a non-template.

--
Craig Powers
MVP - Visual C++

0 new messages