gpt4 book ai didi

c++ - 错误 : no type named 'Unary' in namespace 'ast'

转载 作者:行者123 更新时间:2023-11-28 02:11:45 24 4
gpt4 key购买 nike

我正在尝试为一种实验性编程语言实现抽象语法树,我定义了所有节点类(表达式和语句),问题是我得到了错误:命名空间中没有名为“一元”的类型“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 之间获得一个名称类。更改其中任何一个的名称将解决此问题,或者使表达式 enumenum 类。

关于c++ - 错误 : no type named 'Unary' in namespace 'ast' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35440483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com