例如,“运算符*”不匹配

no match for ‘operator* for instances

本文关键字:运算符 不匹配 例如      更新时间:2023-10-16

我正在尝试使用bison,flex和llvm为某种语言生成代码。以下是联合和规则部分的代码的一部分(点表示与问题无关的代码):

%union {
    Node *node;
    NBlock *block;
    NMethodCall* methodcall;
    .
    .
    .
    std::vector<NIdentifier*> idetListvec;
    std::vector<NRecordItem*> recdListvec;
    .
    .
}
.
.
type           <expr>              numeric      
%type           <expr>              CondExpression
%type           <idetListvec>       IdentifierList
%type           <recdListvec>       RecordItemList  VarDefnList FormalParmSecList   VarDefns FormalParameterList
.
.
%%
.
.
FormalParameterList :  TLPAREN FormalParmSecList  TRPAREN  {$$ = $2;}
                    ;
FormalParmSecList : FormalParmSecList  TSEMICOLON   FormalParmSec   { $1->push_back($3);}
                  | FormalParmSec                                   {$$ = new NRecordItemList(); $$->push_back($1);}
                  ;

FormalParmSec : ValueParmSpec                                       {$$ = $1;}
              | VariableParmSpec                                    {$$ = $1;}
              ;
%%

我有一个单独的文件用于所谓的抽象语法树(AST)来实现这些类。以下是我想问的班级:

#include <iostream>
#include <vector>
class NStatement;
.
classNRecordItem;
classModule;
.
.
typedef std::vector<NRecordItem*> NRecordItemList;
typedef std::vector<NConstantDeclaration*> NConstantDeclarationList;
typedef std::vector<NTypDefn> NTypDefnList;
typedef std::vector<Module> NModuleList;
class Node {
public:
    virtual ~Node() {}
    //virtual llvm::Value* codeGen(CodeGenContext& context) { return NULL; }
};

class NExpression : public Node {
};
class NStatement : public Node {
};
class NInteger : public NExpression {
public:
    int value;
    NInteger(int value) : value(value) { }
    //virtual llvm::Value* codeGen(CodeGenContext& context);
};
.
.
class NRecordItem : public NStatement {
public:
    const NIdentifier& type;
    NIdentifierList arguments;
    NRecordItem(NIdentifierList& arguments, const NIdentifier& type ) :
        type(type),  arguments(arguments) { }
    //virtual llvm::Value* codeGen(CodeGenContext& context);
};

运行代码时,它给我以下错误:

parser.y:256:26: error: no match for ‘operator=’ (operand types are ‘std::vector<NRecordItem*>’ and ‘NRecordItemList* {aka int*}’)
                   | FormalParmSec         {$$ = new NRecordI
                          ^
In file included from /usr/include/c++/5/vector:69:0,
                 from node.h:2,
                 from parser.y:2:
/usr/include/c++/5/bits/vector.tcc:167:5: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = NRecordItem*; _Alloc = std::allocator<NRecordItem*>]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/5/bits/vector.tcc:167:5: note:   no known conversion for argument 1 from ‘NRecordItemList* {aka int*}’ to ‘const std::vector<NRecordItem*>&’
In file included from /usr/include/c++/5/vector:64:0,
                 from node.h:2,
                 from parser.y:2:
/usr/include/c++/5/bits/stl_vector.h:448:7: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = NRecordItem*; _Alloc = std::allocator<NRecordItem*>]
       operator=(vector&& __x) noexcept(_Alloc_traits::_S_not
       ^
/usr/include/c++/5/bits/stl_vector.h:448:7: note:   no known conversion for argument 1 from ‘NRecordItemList* {aka int*}’ to ‘std::vector<NRecordItem*>&&’
/usr/include/c++/5/bits/stl_vector.h:470:7: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = NRecordItem*; _Alloc = std::allocator<NRecordItem*>]
       operator=(initializer_list<value_type> __l)
       ^

我的问题是关于规则"FormalParmSecList"。我将参数的值存储在向量类中,但它给出了该错误。任何想法 !

您正在尝试将std::vector对象直接放入%union中。 这是行不通的,因为它们不会被正确地建造和破坏。 您需要更改%union以使用指针:

%union {
    :
    std::vector<NIdentifier*> *idetListvec;
    std::vector<NRecordItem*> *recdListvec;
您已经在尝试

在操作中存储指向向量的指针,因此看起来您已经在尝试这样做,只是错过了声明。

$$ = new NRecordItemList();

此语句创建指向 NRecordItemList 对象的指针,并将其分配给 $$ 。但是,您在某处声明$$是一个NRecordItemList,而不是指向一个的指针。您需要从此语句中删除new或更改此语法规则的类型声明,以便$$具有正确的类型。我没有看到您在此处发布的代码中在哪里声明了此特定规则的类型。您需要找到并修复它。(自从我使用野牛以来也已经有一段时间了。

您正在尝试分配带有指向std::vector(又名std::vector*)的指针的std::vector

从错误行中删除new以获得{$$ = NRecordItemList(); $$->push_back($1)}