0
/******header file*****/

namespace graph{
     void dfs(...);
};

/******cpp file******/

#include "graph.h"

using namespace graph;

void dfs(...){
     //some code here
     //dfs(...); <-- wrong
     //graph::dfs(...); <-- it was fine,until i call the function from main.cpp
}

When i define the recursive function in the implementation file, it gives me error on the recursive call line. If i change it to "graph::dfs(...)", it doesn't give me error but if i call the function from main.cpp, it still gives me error. If i don't use the "using namespace graph" and call them like "graph::dfs", it doesn't give me error. But why ?

Grey
  • 133
  • 4

1 Answers1

4

When you do using namespace graph; you pull all the symbols from the namespace graph into the current namespace. But it doesn't work the opposite way, it doesn't "push" following global symbols into the graph namespace.

Therefore your function definition is declaring and defining a function dfs in the global namespace.

You need to prefix the function definition with the namespace:

void graph::dfs(...) { ... }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621