c++ - header 中的字符串原型(prototype)需要 iostream?

标签 c++

所以在我的 header 中我创建了一个字符串:string s; 编译器对此感到很吃惊。我知道该字符串是 iostream::std 的一部分。我是否必须将它包含在 header 中而不是 .cpp 文件中?如果是这样,如果使用 header 的其他文件包含 iostream 是否不好?我该如何处理?

编辑:也适用于 vector ,因为我的作业中似乎很需要它。

Edit2:我记得在很多 header 示例中看到过#ifndef,我觉得它可能有助于解决我的问题,但我似乎找不到很好的解释。

最佳答案

stringvectoriostream 是分开的,这三个都是 std 命名空间的一部分。您只需在头文件中适本地限定它们。

作业.h

 #include <string>
 #include <vector>
 // no need to #include <iostream>

 struct Obj
 {
    // fully qualified with std::
    std::string s_;

    // same:
    std::vector<std::string> v_;
 };

关于c++ - header 中的字符串原型(prototype)需要 iostream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840797/

相关文章:

c++ - 将非托管 C++ 库与 .NET API 结合使用

c++ - 不同类的虚函数共享相同的(无效的?)内存地址

c++ - 避免构造函数中 const 引用和 rvalue 引用的指数增长

c++ - 更快地处理 SendARP 函数

c++ - 如何将 libfreenect 数据存储在点云中?

c++ - LNK2005 already defined error on inclusion of C-type header file in C++ project [MSVC12]

c++ - 有没有办法从 istringstream 中读取两个字符?

c++ - 为提升线程命名?

c++ - 源文件翻译的第二阶段?

c++ - JUCE给 “memory.h”找不到