namespace定义在.h中时,不能在另一cpp用?
我在另一个文件中include了那个头文件,随后using了那个名称空间(如using namespace SALES)
报错说我所写的名称空间SALES是并非一个名称空间名,并且我在名称空间中定义的static const int a被报错说没有被声明.
怎么破?
[解决办法]
头文件里写static本身就很搞了.
.h头文件里写:
namespace SALES
{
extern const int a;
extern void function();
}
.cpp源文件里写:
#include ".h"
const int SALES::a = 2;
void SALES::function()
{
return;
}
在其他.cpp中使用该命名空间:
#include ".h"
using namespace SALES;
int main() {cout<<a<<endl; function(); return 0;}
不过不建议using自己写的命名空间, 容易在该.cpp里造成命名查找歧义, 尤其像a这种名字.