Linkage of namespace functions
I have a开发者_Python百科 couple of methods declared at the namespace level within a header for a class:
// MyClass.h
namespace network {
int Method1(double d);
int Method2(double d);
class MyClass
{
//...
}
}
then defined in
//MyClass.cpp
int
Method1(double d)
{ ... }
int
Method2(double d)
{ ... }
This project compiles cleanly and is a dependency for a ui project which uses MyClass. The functions were previously member functions of MyClass, but were moved to namespace since it was more appropriate.
My problem is the ui project complains when it gets to the linker:
1>network.lib(MyClass.obj) : error LNK2001: unresolved external symbol "int __cdecl network::Method1(double)" (?INT@ds@sim@@YAHN@Z)
1>network.lib(MyClass.obj) : error LNK2001: unresolved external symbol "int __cdecl network::Method2(double)" (?CINT@ds@sim@@YAHN@Z)
What am I doing wrong?
It looks like you've put the function declarations inside a namespace block, but forgotten to put the function implementations inside a namespace block as well. Try:
namespace network {
int
Method1(double d)
{ ... }
int
Method2(double d)
{ ... }
}
You need to put the functions in the .cpp file into the namespace, too. The compiler thinks they're two completely different things!
精彩评论