所以在我的 header 中我创建了一个字符串:string s;
编译器对此感到很吃惊。我知道该字符串是 iostream::std 的一部分。我是否必须将它包含在 header 中而不是 .cpp 文件中?如果是这样,如果使用 header 的其他文件包含 iostream 是否不好?我该如何处理?
编辑:也适用于 vector ,因为我的作业中似乎很需要它。
Edit2:我记得在很多 header 示例中看到过#ifndef,我觉得它可能有助于解决我的问题,但我似乎找不到很好的解释。
最佳答案
string
和 vector
与 iostream
是分开的,这三个都是 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/