我写了一些我学到的基本代码,可以用来定义一个获取枚举值作为构造函数参数的类型,并且有一个成员函数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;
}
Run Code Online (Sandbox Code Playgroud)
您不需要包含<iostream>,但是<string>,因为您使用std::string,所以可能会关闭编译器.
如果你包含<string>并仍然得到错误,这听起来像编译器中的错误.
| 归档时间: |
|
| 查看次数: |
393 次 |
| 最近记录: |