我正在尝试为实验性编程语言实现abstrax语法树,我定义了所有节点类(表达式和语句),问题是当我尝试在此处使用任何已定义的类时,不仅是error: no type named 'Unary' in namespace 'ast',我正在获取Unary

#ifndef AST_H
#define AST_H

#include "Commons.h"

namespace ast
{
    enum Statements {
        If,
        IfElse,
        While,
        Assignment,
        VarDeclaration,
        FuncDeclaration,
        SequenceStatement,
        Break,
        Return,
        Continue,
        Null
    };

    enum Expressions {
        Logical,
        Relation,
        Arithmetic,
        Unary,
        SequenceExpression
    };

    enum Operators {
        Or,         // or
        And,        // and
        GE,         // >=
        LE,         // <=
        G,          // >
        L,          // <
        Eq,         // ==
        Neq,        // !=
        Plus,       // +
        Minus,      // -
        Times,      // *
        Div,        // /
        Power,      // **
    };

    // base class of all nodes in the abstrax syntax tree
    class Node {
        public:
            Node() { }
    };

    // base class of all statements
    class Statement: Node {
        public:
            Statement() { }
            Statements type;
    };

    // base class of all expressions
    class Expression: Node {
        public:
            Expression() { }
            Expressions type;
    };

    typedef std::shared_ptr<Expression> ExpressionPtr;
    typedef std::shared_ptr<Statement>  StatementPtr;


    class Identifier: Node {
        public:
            Identifier(const String& identifier, const ExpressionPtr& expression);
            Identifier(const String& identifier);
            String identifier;
            ExpressionPtr expression;
    };
    typedef std::shared_ptr<Identifier> IdentifierPtr;
    typedef std::vector<IdentifierPtr> IdentifierPtrVector;
    typedef std::shared_ptr<IdentifierPtrVector> ParameterListPtr;

    // statements nodes begin
    class If: Statement {
        public:
            If(const ExpressionPtr& expression, const StatementPtr& statements);
            ExpressionPtr expression;
            StatementPtr statements;
    };

    class IfElse: Statement {
        public:
            IfElse(
                    const ExpressionPtr& expression, const StatementPtr& if_statements,
                    const StatementPtr& else_statements);
            ExpressionPtr expression;
            StatementPtr if_statements, else_statements;
    };

    class While: Statement {
        public:
            While(const ExpressionPtr& expression, const StatementPtr& statements);
            ExpressionPtr expression;
            StatementPtr statements;
    };

    class Assignment: Statement {
        public:
            Assignment(const IdentifierPtr& identifier, const ExpressionPtr& expression);
            IdentifierPtr identifier;
            ExpressionPtr expression;
    };

    class VarDeclaration: Statement {
        public:
            VarDeclaration(const IdentifierPtr& identifier, const ExpressionPtr& expression);
            IdentifierPtr identifier;
            ExpressionPtr expression;
    };

    class FuncDeclaration: Statement {
        public:
            FuncDeclaration(const IdentifierPtr& identifier, const ParameterListPtr& parameters,const StatementPtr& statements);
            IdentifierPtr identifier;
            StatementPtr statements;
            ParameterListPtr parameters;
    };

    class Sequence: Statement {
        public:
            Sequence(const StatementPtr& statement, const StatementPtr& statements);
            StatementPtr statement, statements;
    };

    class Break: Statement {
            Break();
    };

    class Continue: Statement {
            Continue();
    };

    class Return: Statement {
        public:
            Return(const ExpressionPtr& expression);
            ExpressionPtr expression;
    };

    // for sequence statements consisting of one statement only.
    class Null: Statement {
            Null();
    };
    // statements nodes end

    // expressions node begin
    class OperatorExpression: Expression {
        public:
            OperatorExpression(const ExpressionPtr& left, const ExpressionPtr& right, const int op);
            ExpressionPtr left, right;
            int op;
    };

    class Unary: Expression {
        public:
            Unary(const int op, const ExpressionPtr& right);
            Operators op;
            ExpressionPtr right;
    };

    class SequenceExpr: Expression {
        public:
            SequenceExpr(const ExpressionPtr& expression, const ExpressionPtr& expressions);
            ExpressionPtr expression, expressions;
    };

}
#endif  /* AST_H */


错误代码:

ast::ExpressionPtr right(new ast::Expression());
auto unary_expr = std::shared_ptr<new ast::Unary(ast::Operators::And, right);


为什么我的编译器无法识别我的类?

最佳答案

您将在名为Unary的类和枚举值Unary.之间获得一个名称类。更改其中一个的名称将解决此问题,或者将表达式enum设置为enum class.

关于c++ - 错误: namespace “ast”中没有名为“Unary”的类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35440483/

10-11 20:04