递归命名空间成员函数定义
recursive namespace member function definition
/******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
}
当我在实现文件中定义递归函数时,它在递归调用行上给我错误。如果我将它更改为 "graph::dfs(...)",它不会给我错误,但是如果我从 main.cpp 调用该函数,它仍然会给我错误。如果我不使用 "using namespace graph" 并像 "graph::dfs" 那样调用它们,它不会给我错误。但是为什么?
当您执行 using namespace graph;
时,您将命名空间 graph
中的所有符号拉入当前命名空间。但它不会以相反的方式工作,它不会 "push" 跟随全局符号进入 graph
命名空间。
因此您的函数定义是在 global 命名空间中声明和定义一个函数 dfs
。
您需要在函数定义前加上命名空间:
void graph::dfs(...) { ... }
/******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
}
当我在实现文件中定义递归函数时,它在递归调用行上给我错误。如果我将它更改为 "graph::dfs(...)",它不会给我错误,但是如果我从 main.cpp 调用该函数,它仍然会给我错误。如果我不使用 "using namespace graph" 并像 "graph::dfs" 那样调用它们,它不会给我错误。但是为什么?
当您执行 using namespace graph;
时,您将命名空间 graph
中的所有符号拉入当前命名空间。但它不会以相反的方式工作,它不会 "push" 跟随全局符号进入 graph
命名空间。
因此您的函数定义是在 global 命名空间中声明和定义一个函数 dfs
。
您需要在函数定义前加上命名空间:
void graph::dfs(...) { ... }