From the comments being made it is clear that this is a bug in VS2005. As no one could provide some insight into the exact source of the problem I will give my solution: I moved the function into a static template class and defined the inner struct as a private local within that class template.
Local struct in template function
-
01-07-2022 - |
Question
The following code reproduces an error that I get in VS2005: I have a template function like
template <typename T> bool foo(T x, T y) {
struct bar {
public:
T t;
bool CompLT(const bar& that) {
return (this->t) < (that.t);
}
};
bar X, Y;
X.t = x;
Y.t = y;
return X.CompLT(Y);
}
in a header file A.h. When I now use the header in two compilation units B.cpp and C.cpp VS2005 complains with the error
error LNK2005: "public: bool __thiscall `bool __cdecl foo<float>(float,float)'::`2'::bar::CompLT(struct `bool __cdecl foo<float>(float,float)'::`2'::bar const &)" (?CompLT@bar@?1???$foo@M@@YA_NMM@Z@QAE_NABU1?1???$foo@M@@YA_NMM@Z@@Z) is already defined in B.obj .
How can I fix this error? Is this a problem with VS2005 or do I have to move the definition of the struct out of the local function scope and make it a template?
Solution 2
OTHER TIPS
Have you used include guards ?
Try adding the following and remove any object file (ending with .o)
#ifndef A_H
#define A_H
//your header
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow