如何在Mac上的高级文本2上运行C ++? [英] How to run C++ on sublime text 2 on a mac?

查看:183
本文介绍了如何在Mac上的高级文本2上运行C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我输入了



<$> p $ p> #includeiostream

int main()
{
cout<<Hello WOrld;
return(0);
}

但它给我一个错误

  /Users/chiragchaplot/q2.cpp:5:2:error:使用未声明的标识符'cout';你的意思是'std :: cout'? 
cout<<Hello World;
^ ~~~
std :: cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ iostream:50:33:note:'std :: cout'这里声明
extern _LIBCPP_FUNC_VIS ostream cout;
^
生成了1个错误。
[退出代码为1的完成时间为0.4s]


解决方案

以下方法可解决您的问题:



在main函数之前添加以下行。

  std; 

因此,您的代码现在将变为:

  #includeiostream

using namespace std;

int main(){
cout<< 你好,世界;
return(0);
}






方法2 :(GOOD PRACTICE)



您可以直接输入 std :: cout $ c> cout 。



std :: cout的完整代码

  #includeiostream

int main(){
std :: cout< ; 你好,世界;
return(0);
}

这告诉编译器 cout 标识符来自 std 命名空间。






方法2比方法1更好。进一步阅读:为什么是using namespace std;






有关命名空间的详细信息,请查看以下链接:


  1. 命名空间维基百科

  2. 有趣的Q / A @ cplusplus.com

  3. 有趣的Q / A @ devshed.com

  4. 教程点


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 (0);
 }

but it gives me an error

/Users/chiragchaplot/q2.cpp:5:2: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
        cout<<"Hello World";
        ^~~~
        std::cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:50:33: note: 'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
                                ^
1 error generated.
[Finished in 0.4s with exit code 1]

解决方案

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

这篇关于如何在Mac上的高级文本2上运行C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆