【问题标题】:Error when i do not include string header file in c++当我在 C++ 中不包含字符串头文件时出错
【发布时间】:2014-01-05 18:33:50
【问题描述】:
#include<iostream>
#include<string>

using namespace std;
void main(){
string str="abc";
cout<<str;
system("pause");
}

如果我不包含字符串头文件,则 行 cout

我认为错误将出现在定义 str 的行。

【问题讨论】:

  • 嗯,不。编译器是自动机,而不是人类。他们报告错误接近其实际位置是完全可以理解的。
  • @H2CO3 但我认为他指的是初始化并想知道为什么它不会在那里发生......
  • @H2CO3 这也是我的解释。
  • void main 不合法。它必须返回int。无论如何,我注意到 MSVC 过去特别喜欢在 &lt;string&gt; 中提供所有内容,除了通过 &lt;iostream&gt; 的 I/O,这有点讽刺。
  • @chris: 类本身是通过ios_base::failure(可能还有其他一些)的构造函数所需的异常来实现的。但是我认为没有引入其他不需要的非成员函数,例如to_string

标签: c++ string std


【解决方案1】:

标准库头文件可以包含其他标准库头文件,即使标准中没有指定。因此,在您的实现中,iostream 标头可能包含string 标头的某些部分,因此std::string 可用但std::operator&lt;&lt;(std::basic_ostream&lt;...&gt;&amp;, const std::basic_string&lt;...&gt;&amp;); 不可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多