g ++表示即使包含适当的标头也不存在函数

时间:2018-10-30 22:46:49

标签: c++ string g++

所以我正在为我的大学从事一些家庭作业,需要将字符串转换为浮点数。无论出于什么原因,g ++都在抱怨'stof'函数不存在。尽管我已经包含了必需的标题。这是我的代码,错误在显示以下内容的行上

holder = stof(x.substr(0,end_of_num));

#include <iostream>
#include <string>
#include <list>

using namespace std;

float process_func(string x);
bool isPartOfNum(char x);

int main() {
    string x;
    while (true) {
        cout << "input a string" << endl;
        getline(cin, x);
        cout << process_func(x);
    }
    return 0;
}

float process_func(string x) {
    int end_of_num =0;// used to find last index from num
    int negMult = 1; //used to multiply value at end if there was a negative
    bool onNum = false; //used to
    list <float> numList;
    list <char> operList;

    if ((x.at(0) < 48 || x.at(0) > 57) && x.at(0) != '-') //check if start of string doesnt have a number or negative symbol
        return -1;
    if (x.at(0) != '-')
        negMult = -1;

    float holder;// temp holder for floats
    int i = 0;
    while (i<x.length()) {
        if (isPartOfNum(x.at(i))) {
            end_of_num++;
            onNum = true;   
        }
        else if (onNum) {
            holder = stof(x.substr(0, end_of_num));
            numList.push_back(holder); //adds num as float to list
            x.erase(0, end_of_num + 1); //+1 removes the space after the number before the operator
            end_of_num = 0;
            onNum = false;
        }
        if (x.at(i) == '+' || x.at(i) == '-' || x.at(i) == '*' || x.at(i) == '/') {
            operList.push_back(x.at(i));
        }
    } //at this point both lists should be full of all needed pieces of info

    int answer = 0;
    int temp;
    bool firstOper=true; // used to hold first operation

    while (numList.size() >=2) { //requires at least 2 entries for last operation
        while (!operList.empty()) {
            temp = numList.front();
            numList.pop_front();
            if (operList.front() == '+') {
                if (firstOper) {
                    answer = temp + numList.front();
                    numList.pop_front();
                    firstOper = false;
                }
                else {
                    answer += temp;
                }
            }
            else if (operList.front() == '-') {
                if (firstOper) {
                    answer = temp - numList.front();
                    numList.pop_front();
                    firstOper = false;
                }
                else {
                    answer -= temp;
                }
            }
            else if (operList.front() == '*') {
                if (firstOper) {
                    answer = temp * numList.front();
                    numList.pop_front();
                    firstOper = false;
                }
                else {
                    answer *= temp;
                }
            }
            else if (operList.front() == '/') {
                if (firstOper) {
                    answer = temp / numList.front();
                    numList.pop_front();
                    firstOper = false;
                }
                else {
                    answer /= temp;
                }
            }
            operList.pop_front();
        }
    }
    return answer;
}

bool isPartOfNum(char x) {
    if ((x >= 48 && x <= 57) || (x == '-' || x == '.'))
        return true;
    return false;
}

1 个答案:

答案 0 :(得分:0)

通过使用c ++ 11编译已解决