此代码的工作,只有当我在9号线取消注释C++ for循环不正常
//else return "null";
,但是这不是我所需要的。我试图更换该行
else continue;
但它也不起作用。 “currentCommand”是一个c风格的字符串。
std::string Parser::dest(){
//Determines whether C-command has a dest mnemonic
bool hasDest = false;
for (int i=0; i<strlen(currentCommand); i++){
if (currentCommand[i] == '='){
hasDest = true;
break;
}
//else return "null";
}
if (hasDest == false) return "null";
std::string destm;
char temp;
int index = 0;
temp = currentCommand[index];
while (temp != '='){
destm += temp;
index++;
}
return destm;
}
我应该当我调用这个函数来得到一个输出,并且我得到它,当我取消//else return "null"
..但它不是我所需要的输出。但是,当我离开这行评论,我没有得到任何输出,并在一段时间后出现此错误:
终止叫做抛出“的std :: bad_alloc的”
的实例以后有什么():STD: :bad_alloc的
1Aborted(核心转储)
如果* currentCommand的第一个字符*不是''='',则返回“null”。可能不是你想要的。如果它*是*你想要的,那么你肯定有一种奇怪的方式来检查数组中的第一个元素...... –
A)为什么你将c风格的字符串与std :: string混合使用? B)什么“不起作用”? C)预期的行为是什么? – Borgleader
你想用这个循环做什么 –