views:

27

answers:

2

I have a 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?

+2  A: 

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)
  { ... }
}
Tyler McHenry
Yes that was it - silly me. On a side note, what effect does static or extern have on namespace functions?
user144182
+1  A: 

You need to put the functions in the .cpp file into the namespace, too. The compiler thinks they're two completely different things!

Mark Ransom