【发布时间】:2013-12-22 04:22:58
【问题描述】:
我不知道出了什么问题。正如我已经注释掉的那样,错误在第 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
}
【问题讨论】:
标签: c++