g++ 说函数不存在,即使包含正确的标头
g++ saying function doesn't exist even when including the proper header
所以我正在为我的uni做一些家庭作业,需要将字符串转换为浮点值。无论出于什么原因,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;
}
使用c++11 编译解决
相关文章:
- 当包含成员函数名为errno和cerrno时,g++7编译错误
- 重载运算符主体仅包含一个函数调用
- std::包含 std::函数回调的多个包装器的向量不起作用
- 扩展包含静态函数的类
- 在包含正整数和负整数的向量中查找最低缺失的整数?
- c++如何在特定文件中包含所有函数而不包含该文件
- 为什么VS C++中的Path包含正斜杠而不是反斜杠
- new[] 一个包含构造函数的字节对象数组没有错,对吧?
- 使用头文件中包含的函数时未定义的引用
- 派生类是否C++必须在头文件中包含继承函数/成员的定义
- 匿名命名空间,用于在两个 cpp 文件中包含一个函数
- 包含虚函数的类可以成为单例
- 返回包含从函数捕获的 lambda
- 如何在单独的文件中包含全局函数
- 当包含全局函数时,c++错误lnk2005
- 与包含复杂函数的内联
- C++,OpenCV:不能包含Cvtcolor函数的imgproc库
- Eclipse CDT - 编辑器无法识别静态库中包含的函数
- 是否可以接受不在标头中包含所有函数声明以创建"private namespace"?
- C++:是否有一个包含各种函数的traits类来操作以零结尾的char*和wchar_t*字符串