gpt4 book ai didi

c++ - 错误 : expected unqualified-id before 'for' . 编译因 -Wfatal-errors 而终止

转载 作者:行者123 更新时间:2023-11-30 02:50:05 25 4
gpt4 key购买 nike

我不知道哪里出了问题。正如我已经注释掉的,错误在第 7 行。非常感谢任何帮助。

错误:'for' 前需要不合格的 id。编译因 -Wfatal-errors 而终止。

#include <iostream>
#include <map>
#include <math.h>

const char digit_ints[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
std::map<char,int> digit_map;
for (int i = 0; i < 10; ++i) // ERROR ON THIS LINE!!!!!!!
digit_map.insert(std::pair<char,int>(digit_ints[i], i));


int str2Int(const std::string& S) {
int sz = S.size();
if (sz == 0) {
std::cout << "str2Int() cannot take an empty string as a parameter." << std::endl;
return -1;
} else {
int sum(0);
for (int j(0), k(sz - 1); j < sz; --k, ++j) {
if ((S[j]) < 0 || (S[j]) > 9) {
std::cout << "str2Int can only take strings with chars '0' through '9' as parameters." << std::endl;
return -1;
} else {
sum += digit_map[S[j]] * pow(10, k);
}
}

}
return sum;
}


int main() {

std::cout << str2Int("3421");

return 0

}

最佳答案

这个 for 循环在函数之外做了什么?

for (int i = 0; i < 10; ++i) // ERROR ON THIS LINE!!!!!!!
digit_map.insert(std::pair<char,int>(digit_ints[i], i));

这肯定一个错误。它不包含在任何函数中。

关于c++ - 错误 : expected unqualified-id before 'for' . 编译因 -Wfatal-errors 而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com