简体   繁体   English

命名空间功能的链接

[英]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. 该项目可以干净地编译,并且是使用MyClass的ui项目的依赖项。 The functions were previously member functions of MyClass, but were moved to namespace since it was more appropriate. 这些函数以前是MyClass的成员函数,但由于更合适而被移到了名称空间。

My problem is the ui project complains when it gets to the linker: 我的问题是ui项目在到达链接器时抱怨:

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):错误LNK2001:无法解析的外部符号“ 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) 1> network.lib(MyClass.obj):错误LNK2001:无法解析的外部符号“ 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. 您还需要将.cpp文件中的函数放入命名空间中。 The compiler thinks they're two completely different things! 编译器认为它们是完全不同的两件事!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM