C + + shortage make namespace enum

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.