mindless.panda
mindless.panda

Reputation: 4092

Linkage of namespace functions

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?

Upvotes: 1

Views: 630

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308168

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

Upvotes: 2

Tyler McHenry
Tyler McHenry

Reputation: 76660

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)
  { ... }
}

Upvotes: 3

Related Questions