简体   繁体   English

如何在不同的命名空间中调用函数?

[英]How do I call a function in a different namespace?

I have a function called test in namespace buzz . 我在namespace buzz有一个名为test的函数。

From this test function i am calling another function called dummy which is inside namespace example . 从这个测试函数我调用另一个名为dummy函数,它在namespace example

I get the following error: 我收到以下错误:

Dummy is not a member of example. 虚拟不是示例的成员。

Can you please tell me how to communicate between 2 different namespaces? 你能告诉我如何在两个不同的命名空间之间进行通信吗?

Thanks 谢谢

If the namespace is not nested, you should start navigating from the root one, ie: 如果命名空间不是嵌套的,则应该从根目录开始导航,即:

Instead of: 代替:

example::dummy

Write: 写:

::example::dummy

Following code works with gcc (as expected). 以下代码适用于gcc(如预期的那样)。 Your problem must be with something that is not in the question. 你的问题必须是问题中没有的东西。

#include <iostream>

namespace example
{
  void dummy() { std::cout << "Dummy\n"; }
}

namespace buzz
{
  void test() { example::dummy(); }
}

int main()
{
  buzz::test();
}

You need to provide code for this query. 您需要为此查询提供代码。 Otherwise just from your question, I guess you are making spelling error: 否则只是从你的问题,我猜你正在拼写错误:

namespace example {
  void dummy() {}
}
namespace buzz {
  void test () { example::Dummy(); }  // capital 'D' instead of 'd' for dummy
}

Naturally, Dummy is not a member of example. 当然, Dummy is not a member of example. :)) :))

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

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