Hello everyone,
I am meeting with a strange linking error regarding to operator overloading. I am creating a C++ static lib, and then linking it with a C application. Here is the error message and source codes,
I am using adapter.cpp, adapter.h, foo.cpp and foo.h to create a static lib called foo.lib, then link it with a C application (goo.c). There is no problem to generate the static lib foo.lib.
Any comments to solve this link error?
Code:
error LNK2019: unresolved external symbol "private: class FooNameSpace::Foo __thiscall FooNameSpace::Foo::operator=(long)" (??4Foo@FooNameSpace@@AAE?AV01@J@Z) referenced in function "private: __thiscall FooNameSpace::Foo::Foo(
long)" (??0Foo@FooNameSpace@@AAE@J@Z)
fatal error LNK1120: 1 unresolved externals
adapter.cpp
Code:
#include "adapter.h"
#include "foo.h"
using namespace FooNameSpace;
void compute(void)
{
Goo g;
g.computeFoo();
}
adapter.h
Code:
#ifdef __cplusplus
extern "C" {
#endif
void compute (void);
#ifdef __cplusplus
}
#endif
foo.cpp
Code:
#include "foo.h"
using namespace FooNameSpace;
Foo Foo::operator=(long value)
{
this -> value = value;
return (*this);
}
foo.h
Code:
namespace FooNameSpace
{
class Foo
{
private:
long value;
Foo (long value) { (*this) = value; }
inline Foo operator= (long value);
friend class Goo;
};
class Goo
{
public:
void computeFoo()
{
Foo foo = Foo(0L);
}
};
}
goo.c (link with foo.lib)
Code:
#include "adapter.h"
#include "windows.h"
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
{
compute();
return 0;
}
thanks in advance,
George