函数之前的预期初始值设定项(易于修复?

Expected Initializer Before Function (Easy Fix?)

本文关键字:易于修 函数      更新时间:2023-10-16

嘿伙计们,我对整个C++的事情很陌生,我目前正在编写一个程序,其中我收到来自 java 的错误"错误:'函数'之前的预期初始值设定项"我真的不明白我做错了什么,因为我向 doMath 传递了一个字符串,然后尝试使用该字符串。我尝试将从 .at(0( 获得的内容存储在字符串和字符中,但都没有奏效。任何帮助将不胜感激!

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
//pass string with leading operator and arguments
int doMath(const string function){
int answer = -1;
char operator = function.at(0);
if(operator.compare("*") == 0){
for(int i = 1; i < function.length(); i++){
if(i < 1){
answer = stoi(function.at(i));
}
else{
answer = answer * stoi(function.at(i));
}
}
}
//etc. etc. etc.
}`

operator是C++中的关键字。你应该称你的变量为别的东西。

希望如果您使用带有语法突出显示的编辑器,它将突出显示关键字。