从枚举转换为int [英] converting from enum to int
本文介绍了从枚举转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已声明以下枚举:
enum periods {one, five, ten, fifteen, thirty};
现在我想在主函数中将其作为命令行参数传递。
and now I want to pass it as a commandline argument in my main function.
int main(int argc, char* argv[]) {
periods mp;
if (argc == 2) {
std::string min_prd(argv[2]);
mp=atoi(min_prd.c_str());
}
并且编译器提示:
error: invalid conversion from ‘int’ to ‘periods’
$ b b
我做了atoi(),因为我认为枚举是一个int - 如何修复我的代码?
I did atoi() because I figured enum is an int - how do I fix my code?
推荐答案
必须显式转换:
mp=static_cast<periods>(atoi(min_prd.c_str()));
这篇关于从枚举转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文