How to run C++ on sublime text 2 on a mac?

前端 未结 1 534
青春惊慌失措
青春惊慌失措 2021-01-26 17:31

I tried to run hello world in C++ on sublime text 2 on my mac.

I typed

#include \"iostream\"

 int main()
 {
   cout<<\"Hello WOrld\";
   return (         


        
相关标签:
1条回答
  • 2021-01-26 17:56

    The following methods will solve your problem:


    Method 1 : (BAD PRACTICE)

    Adding the following line before main function.

    using namespace std;
    

    So your code will now become:

    #include "iostream"
    
    using namespace std;
    
    int main(){
       cout << "Hello WOrld";
       return (0);
    }
    

    Method 2 : (GOOD PRACTICE)

    You can simply write std::cout instead of cout.

    Full code with std::cout

    #include "iostream"
    
    int main(){
       std :: cout << "Hello WOrld";
       return (0);
    }
    

    This tells the compiler that the cout identifier comes from the std namespace.


    Method 2 is better than Method 1. Further reading : Why is "using namespace std" considered bad practice?


    For more information on namespaces, check out the following links:

    1. Namespaces Wikipedia
    2. Interesting Q/A @cplusplus.com
    3. Interesting Q/A @devshed.com
    4. Tutorials Point
    0 讨论(0)
提交回复
热议问题