namespace
写了两个命名空间,里面有同名的函数
[root@linux tmp]# cat main.cpp
#include <iostream>
namespace space1
{
void fun(void)
{
std::cout<<"space1 \n";
}
}
namespace space2
{
void fun(void)
{
std::cout<<"space2 \n";
}
}
int main(void)
{
fun();
return 0;
}
没有指定命名空间,编译期间报错:
[root@linux tmp]# g++ main.cpp && ./a.out
main.cpp: In function ‘int main()’:
main.cpp:22: error: ‘fun’ was not declared in this scope
[root@linux tmp]#using 一个命名空间
[root@linux tmp]# cat main.cpp
#include <iostream>
namespace space1
{
void fun(void)
{
std::cout<<"space1 \n";
}
}
namespace space2
{
void fun(void)
{
std::cout<<"space2 \n";
}
}
using namespace space1;
int main(void)
{
fun();
return 0;
}
[root@linux tmp]# g++ main.cpp && ./a.out
space1
[root@linux tmp]#手动指定命名空间:
[root@linux tmp]# cat main.cpp
#include <iostream>
namespace space1
{
void fun(void)
{
std::cout<<"space1 \n";
}
}
namespace space2
{
void fun(void)
{
std::cout<<"space2 \n";
}
}
int main(void)
{
space1::fun();
space2::fun();
return 0;
}
[root@linux tmp]# g++ -Wall main.cpp && ./a.out
space1
space2
[root@linux tmp]#枚举
[root@linux tmp]# cat main.c
# include <stdio.h>
enum weekday
{
sun,
mon,
tue,
wed,
thu,
fri,
sat
} day;
int main()
{
int k;
printf("input a number(0--6) ");
scanf("%d",&k);
day=(enum weekday)k;
switch(day)
{
case sun:printf("sunday\n");break;
case mon:printf("monday\n");break;
case tue:printf("tuesday\n");break;
case wed:printf("wednesday\n");break;
case thu:printf("thursday\n");break;
case fri:printf("friday\n");break;
case sat:printf("satday\n");break;
default:printf("input error\n");break;
}
return 0;
}
[root@linux tmp]# gcc main.c && ./a.out
input a number(0--6) 2
tuesday
[root@linux tmp]# gcc main.c && ./a.out
input a number(0--6) 0
sunday
[root@linux tmp]#本文出自 “魂斗罗” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1864328
原文:http://990487026.blog.51cto.com/10133282/1864328