The error message indicates that the function FindPosition has not been declared or defined before it is used in line 9 of the code. To fix this error, the FindPosition function should be declared or defined before it is used in the strToInt function.

One way to do this is to add a function prototype for FindPosition before the strToInt function, like this:

class Solution {
public:
    int strToInt(string str);
    int FindPosition(int l, int r, string str);
};

This tells the compiler that there is a function called FindPosition that takes three arguments of type int and string, and returns an int. Then, the strToInt function can use this function without any errors.

Alternatively, the FindPosition function can be defined before the strToInt function, like this:

class Solution {
public:
    int FindPosition(int l, int r, string str) {
        int pos = 0;
        for(int i = l; i <= r; i++) {
            if(str[i] < '9' && str[i] > '0') pos = i;
            else break;
        }
        return pos;
    }

    int strToInt(string str) {
        int l = 0, r = str.size() - 1;
        int pos = 0;
        while(str[l] != ' ') l++;
        while(str[r] != ' ') r--;
        if(str[l] == '-' || str[l] == '+') {
            if(FindPosition(l + 1, r, str) == 0) return 0;
            string s = str.substr(l + 1, FindPosition(l + 1, r, str));
            if(str[l] == '-') return -(s - '0');
            else return s - '0';
        } else {
            if(FindPosition(l, r, str) == 0) return 0;
            string s = str.substr(l, FindPosition(l, r, str));
            return s - '0';
        }
    }
};

This defines the FindPosition function before the strToInt function, so it can be used without any errors


原文地址: http://www.cveoy.top/t/topic/fHd5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录