错误:令牌之前'.'预期的主表达式
Error: expected primary expression before '.' token
我试图编写代码来检查输入字符串中的括号对,并打印出"成功"(对于具有匹配对的输入)或第一个不匹配的结束括号的基于1的索引。
但是我得到一个错误:
期望在'之前的主表达式。' token
,当我编译。
#include <iostream>
#include <stack>
#include <string>
struct Bracket {
Bracket(char type, int position):
type(type),
position(position)
{}
bool Matchc(char c) {
if (type == '[' && c == ']')
return true;
if (type == '{' && c == '}')
return true;
if (type == '(' && c == ')')
return true;
return false;
}
char type;
int position;
};
int main() {
std::string text;
getline(std::cin, text);
int z;
std::stack <Bracket> opening_brackets_stack;
for (int position = 0; position < text.length(); ++position) {
char next = text[position];
if (next == '(' || next == '[' || next == '{') {
opening_brackets_stack.push(Bracket(next,0));
}
if (next == ')' || next == ']' || next == '}') {
if(Bracket.Matchc(next) == false || opening_brackets_stack.empty() == false)
{
z = position;
}
else
{
opening_brackets_stack.pop();
}
}
}
if (opening_brackets_stack.empty())
{
std::cout << "Success";
}
else
{
std::cout << z;
}
return 0;
}
Reason -您直接使用类Bracket
而不是对象。
方案-
要创建一个对象,需要在程序中包含以下代码:
即. .
在您的main
中,包含以下语句来创建一个括号对象。
Bracket brackObj(next, 0);
现在,将这个特定对象包含在stack
if (next == '(' || next == '[' || next == '{') {
opening_brackets_stack.push(brackObj);
}
现在,您可以在同一个对象上调用您的方法Matchc
。
if(brackObj.Matchc(next) == false ......
相关文章:
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- (错误:令牌之前'<<'预期的主表达式)
- 错误:C++中']'令牌之前的预期主表达式
- 错误:令牌之前的预期主表达式'&&'
- 令牌之前']'预期的主表达式?
- 错误:令牌之前的预期主表达式'/'
- 正则表达式使用提升令牌迭代器在单引号和括号之间提取值
- 令牌之前')'预期的主表达式(需要"template"消除歧义器)
- 错误:令牌之前的预期主表达式'<':Arduino RGB 舞灯
- 令牌之前出现错误')'主表达式
- C++错误:错误:令牌之前的预期主表达式'||'
- 令牌问题之前'*'预期的主表达式
- 令牌之前的预期表达式']'?C
- 退出状态 1 令牌之前的预期主表达式'.'
- C++错误:";"之前应为主表达式使用Cmake的令牌
- 向量空Push_back在令牌之前调用'{'预期的主表达式
- 令牌调用之前'...'预期的主表达式 模板化类的模板化方法
- 错误 预期主表达式在 ';" 之前令牌 C++
- 令牌之前的预期主表达式'-'
- 令牌之前的预期主表达式'<'