我正在尝试在攻击向量上使用foreach循环,每个攻击都有一个唯一ID,比如1-3。
类方法会接受键盘输入的1-3。
我正在尝试使用foreach来遍历m_attack中的元素,查看数字是否匹配,如果匹配...做某些事情。
我遇到的问题是:
a'for each' statement cannot operate on an expression of type "std::vector<Attack
我这样做是完全错误的吗?我有C#的经验,这在某种程度上是我依据的,任何帮助将不胜感激。
我的代码如下:
在头部
vector<Attack> m_attack;
在课堂上
int Player::useAttack (int input)
{
for each (Attack* attack in m_attack) // Problem part
{
//Psuedo for following action
if (attack->m_num == input)
{
//For the found attack, do it's damage
attack->makeDamage();
}
}
}
std::map<int, Attack*> attacks;
,在方法中:attacks[input]->makeDamage();
(在out_of_range
异常的 try catch 中)。 - cprn