Namespace
I wrote two namespaces with the same name function.
[email protected] 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;} No namespace specified, error during compilation: [[email protected] tmp]# g++ main.cpp &&./a.out main.cpp:In function ' int main () ': main.cpp:22: Error: ' Fun ' is not declared in this scope[[email protected] tmp]#
Using a Namespace
[email protected] 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;} [Email protected] tmp]# g++ main.cpp &&/a.out space1 [[email protected] tmp]#
To specify namespaces manually:
[email protected] 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;} [Email protected] tmp]# g++-wall main.cpp &&/a.out space1 space2 [[email protected] tmp]#
Enumeration
[[Email protected] 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);d ay= (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 :p rintf ("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;} [[email protected] tmp]# gcc main.c && ./a.out input a number (0--6) 2tuesday[[email protected] tmp]# gcc main.c && . /a.out input a number (0--6) 0sunday[[email protected] tmp]#
This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1864328
C + + shortage make namespace enum