为什么我的代码需要<iostream>?
Why does my code need <iostream>?
我编写了一些我学到的基本代码,可用于定义一个类型,该类型获取枚举值作为其构造函数参数,并具有一个以字符串形式返回值的成员函数AsString()
。
除非我包含<iostream>
,否则代码无法编译。它main
显示一条警告,指出尚未声明类型 color
。为什么需要在我的代码中包含输入/输出头文件,而其中没有使用输入/输出函数或运算符?
enum ColorEnum {blue, red};
class color
{
protected:
ColorEnum value;
public:
color(ColorEnum initvalue)
{
value = initvalue;
}
std::string AsString()
{
switch (value)
{
case blue:
return "blue";
case red:
return "red";
default:
return "N/A";
}
}
};
int main()
{
color mycolor = blue;
return 0;
}
你不需要<iostream>
,你需要<string>
才能std::string
,你可能通过<iostream>
间接获得。
你不需要包含<iostream>
,但<string>
,因为你使用std::string
,所以这可能会使编译器关闭。
如果您包含<string>
但仍然收到错误,这听起来像是编译器中的错误。
相关文章:
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话
- 需要从 istream 和 ostream 派生 iostream
- EASTL矢量<向量<int>>连续的
- std::带有自定义缓冲区的 iostream 不允许我写入
- 新的放置取决于 iostream
- 包含在 <initializer_list> <iostream>?
- 为什么在包含iostream时可以使用printf()?
- 如果 iostream 对象不可复制,为什么以下代码是合法的?
- 为什么我必须在包含后写 std::cin <iostream>?
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- 磁力计代码:C++ vs C - iosstream,矢量 - 错误iostream:没有这样的文件或目录
- 通过boost asio iostream下载大文件的最快方法是什么?
- 为什么 VScode 在 .h 文件中显示"'iostream' file not found"?
- 包含iostream导致不同的二进制文件
- iostream运算符重载的C++异常规范
- basic_iostream和iostream之间的区别
- iostream 和 fstream 的重载<<
- Borland C++编译器中的Iostream
- 从类模板继承时需要iostream
- 为什么<iostream>依赖MinGW dll?