错误:命名空间'ast'中没有名为'Unary'的类型

时间:2016-02-16 18:35:34

标签: c++ c++11

我正在尝试为实验性编程语言实现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);

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

1 个答案:

答案 0 :(得分:2)

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