[英]I'm getting an "Error: string was not declared in this scope"
我在 bool 函數中聲明了一個字符串參數。 當我運行代碼構建消息時,顯示“字符串未在范圍內聲明”。
我在代碼塊上試過..
bool isOkay(char open,char close);
bool isCheck(string exp);
bool isCheck(string exp){
stack<char>s;
int i;
for(i=0;i<exp.length();i++){
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '['){
s.push(exp[i]);
}
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){
if(s.empty() || !isOkay(s.top(),exp[i])){
return false;
}
else{
s.pop();
}
}
}
return s.empty() ? true : false;
}
bool isOkay(char open , char close){
if(open == '(' && close== ')') return true;
else if(open == '{' && close== '}') return true;
else if(open == '[' && close== ']') return true;
return false;
}
錯誤消息是“字符串未在范圍內聲明”
你沒有添加using namespace std; 在你的程序中。 添加使用命名空間 std后,我在代碼塊中成功編譯了它; .
這是完整的代碼:
#include <iostream>
#include <string>
#include <stack>
using namespace std; // May be you didn't write this line.
bool isOkay(char open,char close);
bool isCheck(string exp);
int main() {
// main func code goes here
return 0;
}
bool isCheck(string exp){
stack<char>s;
int i;
for(i=0;i<exp.length();i++){
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '['){
s.push(exp[i]);
}
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){
if(s.empty() || !isOkay(s.top(),exp[i])){
return false;
}
else{
s.pop();
}
}
}
return s.empty() ? true : false;
}
bool isOkay(char open , char close){
if(open == '(' && close == ')') return true;
else if(open == '{' && close == '}') return true;
else if(open == '[' && close == ']') return true;
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.