程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C++ LLVM unique_ptr 问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决C++ LLVM unique_ptr 问题?

开发过程中遇到C++ LLVM unique_ptr 问题的问题如何解决?下面主要结合日常开发的经验,给出你关于C++ LLVM unique_ptr 问题的解决方法建议,希望对你解决C++ LLVM unique_ptr 问题有所启发或帮助;

老实说,我不知道在这里做什么。 当我遇到这个函数时,我正在关注 C++ (https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl02.html) 的这个 LLVM 教程:

static std::unique_ptr<ExprAST> ParsenumberExpr() {
  auto Result = std::make_unique<numberExprAST>(NumVal);
  getNextToken(); // consume the number
  return std::move(Result);
}

它在函数的最后一行给我一个错误,我无法将 unique_ptr<numberExprAST> 转换为 unique_ptr<ExprAST>numberExprAST 是 ExprAST 的子代),我想这个错误是有道理的,但是在教程中这是怎么回事呢?我逐字逐句地复制了函数(我什至几乎逐字地复制了类),但我仍然遇到错误。如果其他人看过本教程,他们在这种情况下做了什么?

编辑:我被要求展示 AST 文件的完整代码,所以这里是:

#include <String>
#include <vector>
#include <memory>
#include "lexer.hpp"

class ExprAST
{
public:
    virtual ~ExprAST() {}
};

class NumExprAST : ExprAST
{
private:
    double m_val;
public:
    NumExprAST(const double val) {m_val = val;}
};

class VarExprAST : ExprAST
{
private:
    std::string m_name;
public:
    VarExprAST(const std::string &Name) {m_name = name;}
};

class BinoprExprAST : ExprAST
{
private:
    char m_opr;
    std::unique_ptr<ExprAST> m_lhs,m_rhs;
public:
    BinoprExprAST(char opr,std::unique_ptr<ExprAST> lhs,std::unique_ptr<ExprAST> rhs)
    {
        m_opr = opr;
        m_lhs = std::move(lhs);
        m_rhs = std::move(rhs);
    }
};

class CallExprAST : public ExprAST {
private:
  std::string m_callee;
  std::vector<std::unique_ptr<ExprAST>> m_args;
public:
    CallExprAST(const std::string &callee,std::vector<std::unique_ptr<ExprAST>> args)
    {
        m_callee = callee;
        m_args = args;
    }
};

class PrototypeAST {
    std::string m_name;
    std::vector<std::string> m_args;

public:
    PrototypeAST(const std::string &name,std::vector<std::string> args)
    {
        m_name = name;
        m_args = args;
    }

    const std::string &getname() const {return m_name;}
};

class FunctionAST {
    std::unique_ptr<PrototypeAST> m_proto;
    std::unique_ptr<ExprAST> m_body;

public:
    FunctionAST(std::unique_ptr<PrototypeAST> proto,std::unique_ptr<ExprAST> body)
    {
        m_proto = std::move(proto);
        m_body = std::move(body);
    }
};

static int currTok;
static int getNextToken() {return currTok = getToken();}

std::unique_ptr<ExprAST> logErr(const std::string errStr)
{
    std::cerr << "log error: " << errStr << "\n";
    return nullptr;
}
std::unique_ptr<PrototypeAST> logErrP(const std::string errStr)
{
    logErr(errStr);
    return nullptr;
}

static std::unique_ptr<ExprAST> parseNumExpr()
{
    auto result = std::make_unique<NumExprAST>(g_numVal);
    getNextToken();
    return std::move(result);
}

我收到的错误信息是这样的

no suitable user-defined conversion from "std::unique_ptr<NumExprAST,std::default_delete<NumExprAST>>" to "std::unique_ptr<ExprAST,std::default_delete<ExprAST>>" exists

错误消息来自 VSCode 而不是终端,因此它可能与标准 C++ 错误消息不完全相同。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的C++ LLVM unique_ptr 问题全部内容,希望文章能够帮你解决C++ LLVM unique_ptr 问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。