【发布时间】:2015-06-30 12:52:00
【问题描述】:
我尝试将枚举元素打印为文本而不是数字。代码如下:
#include <iostream>
#include <unordered_set>
enum enm{
One,
Two
};
class Complex{
public:
void func(std::unordered_multiset<int> _v);
void name(std::unordered_multiset<int>::const_iterator i);
};
void Complex:: name(std::unordered_multiset<int>::const_iterator i){
switch(*i) {
case One:
std::cout<<"One"<<std::endl;
break;
case Two:
std::cout<<"Two"<<std::endl;
break;
}
}
void Complex:: func(std::unordered_multiset<int> _v){
_v.insert(One);
_v.insert(Two);
for (std::unordered_multiset<int>::const_iterator i(_v.begin()), end(_v.end()); i != end; ++i){
std::cout<<"Res: "<<name(i)<<endl;
}
}
int main(){
Complex c;
std::unordered_multiset<int> ms;
c.func(ms);
return 0;
}
我有一个函数name(std::unordered_multiset<int>::const_iterator i),在迭代器上使用开关。问题是 - 它失败了。但我不知道如何解决这个问题以及为什么会发生。
编辑:
我有:
标准::std::cout<<"Res: "<<name(i)<<std::endl;
不仅:name(i);
解决方案
因此,它在以下情况下有效:
std::cout<<"Res: "<<std::endl;
name(i);
【问题讨论】:
-
“它失败了” 怎么样? Runs fine for me
-
我明白了——错误:形成对 void 的引用
-
这不是你昨天问的same question吗?
-
什么编译器?您可以编辑您的帖子以包含 完整 错误消息
-
巴里,这是另一个版本,没有超载
标签: c++ enums stl iterator switch-statement