Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a function in a different namespace?

Tags:

c++

namespaces

I have a function called testin namespace buzz.

From this test function i am calling another function called dummy which is inside 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

like image 558
SPB Avatar asked Apr 11 '11 09:04

SPB


3 Answers

If the namespace is not nested, you should start navigating from the root one, i.e.:

Instead of:

example::dummy

Write:

::example::dummy
like image 41
sukru Avatar answered Oct 10 '22 13:10

sukru


Following code works with gcc (as expected). 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();
}
like image 86
stefaanv Avatar answered Oct 10 '22 14:10

stefaanv


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. :))

like image 39
iammilind Avatar answered Oct 10 '22 15:10

iammilind