Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:在';之前应为非限定id;对于';。编译因-Wfatal错误而终止_C++ - Fatal编程技术网

C++ 错误:在';之前应为非限定id;对于';。编译因-Wfatal错误而终止

C++ 错误:在';之前应为非限定id;对于';。编译因-Wfatal错误而终止,c++,C++,我想不出怎么了。错误出现在第7行,我已经注释掉了。非常感谢您的帮助 错误:在“for”之前应为非限定id。 编译因-Wfatal错误而终止。 #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> digi

我想不出怎么了。错误出现在第7行,我已经注释掉了。非常感谢您的帮助

错误:在“for”之前应为非限定id。 编译因-Wfatal错误而终止。

#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

}
#包括
#包括
#包括
const char digital_ints[]={'0','1','2','3','4','5','6','7','8','9'};
标准::地图数字\地图;
for(int i=0;i<10;++i)//这行有错误!!!!!!!
数字映射插入(标准::对(数字映射[i],i));
int str2Int(const std::string&S){
int sz=S.size();
如果(sz==0){

std::cout对于
循环在函数之外做什么

for (int i = 0; i < 10; ++i) // ERROR ON THIS LINE!!!!!!!
    digit_map.insert(std::pair<char,int>(digit_ints[i], i));
for(int i=0;i<10;++i)//此行出错!!!!!!!
数字映射插入(标准::对(数字映射[i],i));

这当然是一个错误。它不包含在任何函数中。

否则我应该如何获取映射作为全局变量?我需要将映射用于str2Int函数,我不希望每次调用该函数时都实例化它。将for循环放在单独的函数中,如Initialize()或者别的什么。然后在必要时单独调用它。语句,例如
for
语句,不允许在函数体之外。在C中,您必须将循环放在函数内部,然后显式调用它——或者,对于这样简单的事情,您可以将
for
循环移动到
main