Class 没有命名 bison 中的类型

Class does not name a type in bison

这是 parser.ypp 文件的开头

%{
    #include <iostream>
    #include "hw3_output.hpp"   
    using namespace std;
    using namespace output;
    extern int yylineno;
    extern int yylex();
    int yyparse();
    void output::errorSyn(int lineno);
    void yyerror(char const* s);
    output::TableStack* SymboleTableStack = new output::TableStack();
    SymboleTableStack->makeTable();
    output::offsetStack* st = new output::offsetStack();
    st->push(0);
%}

这是我的 hw3_output.hpp 文件。我只展示重要的部分:

    namespace output{
    class SymbolTable {
            vector<std::shared_ptr<SymbolNode>> Symbols;
        public:
            SymbolTable() : Symbols() {}
            void AddSymbolToTable(std::shared_ptr<SymbolNode> symbolNode) {
                Symbols.push_back(symbolNode);
            }
    ....
class TableStack {
    public:
        vector<SymbolTable*> stack;
        TableStack() :stack() {}
        SymbolTable* topTable()
        {
            return *(stack.begin() + stack.size() - 1);
        }
    class offsetStack
        {
        public:
            vector<int> stack;
            offsetStack() :stack() {}

        };
    }

I keep getting the following error (and the same for the offsetStack)

您粘贴的代码位于生成文件的顶层(例如,这使得使用 #include 指令成为可能)。回想一下,C++ 程序的顶层只能包含声明和定义,而不能包含可执行语句。并且

SymboleTableStack->makeTable();

st->push(0);

是可执行语句。

您需要在某些函数或构造函数中进行初始化。