C++的命名空间

假如两个头文件中均定义了类Cal,而调用程序同时包含了两个头文件,当在定义Cal c时,程序会报类型重定义的错误.
这种问题可以通过命名空间来解决.

//head1.h
1
namespace NS1 2 { 3 class Cal 4 { 5 int add(int a, int b); 6 }; 7 } 8 9 //head2.h 10 namespace NS2 11 { 12 class Cal 13 { 14 int multi(int a, int b); 15 }; 16 }

//main.cpp
1 #include "head1.h"
2 #include "head2.h"
3 
4 int main()
5 {
6     NS2::Cal c;
7     return 0;
8 }

猜你喜欢

转载自www.cnblogs.com/Stephen-Qin/p/11709806.html
今日推荐