简体   繁体   English

C ++ #include <iostream>

[英]C++ #include <iostream>

I'm a complete newbie to C++. 我是C ++的新手。 I'm trying to write a simple c++ program but I got an error message. 我正在尝试编写一个简单的C ++程序,但收到一条错误消息。 I suspect this is due to me accidentally deleting some .h files on my mac which might have ruined my Clang compiler. 我怀疑这是由于我不小心删除了Mac上的某些.h文件,这可能毁了我的Clang编译器。 How can I fix this? 我怎样才能解决这个问题? Do I need to reinstall Xcode or change a compiler? 我是否需要重新安装Xcode或更改编译器?

Error message from terminal: 来自终端的错误消息:

    192:desktop ivanlee$ gcc test.cpp
    In file included from test.cpp:1:
    In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:38:
    In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
    In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:90:
    In file included from /usr/include/wchar.h:70:
    In file included from /usr/include/_types.h:27:
    /usr/include/sys/_types.h:32:10: fatal error: 'sys/cdefs.h' file not found
        #include <sys/cdefs.h>
             ^
    1 error generated.

Code: 码:

#include <iostream>

int main(){
    return 0;
    std::cout << "Hey";
}

At first I ought to recommend you the definitive stackoverflow c++ books list . 首先,我应该向您推荐权威的stackoverflow c ++书籍清单 Read these books and get your skills grow. 阅读这些书,使您的技能得到发展。 This also will prevent questions like this. 这样也可以避免此类问题。

Your question needs very-very basic knowledges and answers may be too long and your problem can be solved by many different methods. 您的问题需要非常非常的基础知识,答案可能太长,可以通过许多不同的方法来解决您的问题。

I can tell you one but you should not ask questions like this. 我可以告诉你一个,但你不应该问这样的问题。

Answer 回答

Your code contains a mistakes: 您的代码包含错误:

  1. return 0 before your other instructions (it should be the last). 在其他指令之前return 0 (应该是最后一条)。 Now your program will just do nothing. 现在,您的程序将什么都不做。

  2. You should always compile C++ code with C++ compiler. 您应该始终使用C ++编译器来编译C ++代码。 gcc is not a c++ compiler but c compiler - use g++ instead. gcc不是c++编译器,而是c编译器-请改用g++

  3. Even if you correct 2 errors above, your std::cout call may fail because it does not flush the stream. 即使您纠正了上面的2个错误,您的std::cout调用也可能会失败,因为它不会刷新流。 You should also add << std::endl to this call. 您还应该在此调用中添加<< std::endl

Execute 执行

g++ test.cpp -o out

Instead of 代替

gcc text.cpp
run Terminal
execute gcc -v

Read the info and copy the include path. 阅读信息并复制包含路径。 Copy it to your IDE that allows you to add the include path. 将其复制到您的IDE,该IDE允许您添加包含路径。

Mine, for an example, is: 例如,我的是:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM