Compilation error: invalid conversion when calling base-type constructor

Compilation error: invalid conversion when calling base-type constructor

我正在尝试在 MinGW-64 lljvm 下编译,这是一个不太大的程序。但是我陷入了一个让我发疯的错误。

代码如下:

backend.h

class JVMWritter {
...
static char id;
...
};

backend.cpp

char JVMWriter::id = 0;

JVMWriter::JVMWriter(): FunctionPass(&id) {}

现在,FunctionPass 在 Pass.h 中声明如下:

Pass.h

class FunctionPass {
...
explicit FunctionPass (char &pid) : <initialization here> {}
...
};

当我尝试编译它时,我不断收到以下错误:

backend.cpp:39:16: error: invalid conversion from 'char*' to 'char' [-fpermissive]
   FunctionPass(&JVMWriter::id), targetData(td), out(o), classname(cls), debug(dbg) {
                ^~~~~~~~~~

In file included from D:/dev/msys64/mingw64/include/llvm/IR/DataLayout.h:27:0,
                 from D:/dev/msys64/mingw64/include/llvm/IR/Module.h:20,
                 from D:/dev/msys64/mingw64/include/llvm/IR/PassManager.h:46,
                 from D:/dev/msys64/mingw64/include/llvm/Analysis/LoopInfo.h:47,
                 from backend.h:29,
                 from backend.cpp:23:
D:/dev/msys64/mingw64/include/llvm/Pass.h:300:12: note:   initializing argument 1 of 'llvm::FunctionPass::FunctionPass(char&)'
   explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
            ^~~~~~~~~~~~

backend.cpp:39:16: error: cannot bind rvalue '(char)(& JVMWriter::id)' to 'char&'
   FunctionPass(&JVMWriter::id), targetData(td), out(o), classname(cls), debug(dbg) {
                ^~~~~~~~~~

In file included from D:/dev/msys64/mingw64/include/llvm/Pass.h:388:0,
                 from D:/dev/msys64/mingw64/include/llvm/IR/DataLayout.h:27,
                 from D:/dev/msys64/mingw64/include/llvm/IR/Module.h:20,
                 from D:/dev/msys64/mingw64/include/llvm/IR/PassManager.h:46,
                 from D:/dev/msys64/mingw64/include/llvm/Analysis/LoopInfo.h:47,
                 from backend.h:29,
                 from backend.cpp:23:
D:/dev/msys64/mingw64/include/llvm/PassAnalysisSupport.h: In instantiation of 'llvm::AnalysisUsage& llvm::AnalysisUsage::addRequired() [with PassClass = llvm::LoopInfo]':

backend.cpp:52:27:   required from here
D:/dev/msys64/mingw64/include/llvm/PassAnalysisSupport.h:61:39: error: 'ID' is not a member of 'llvm::LoopInfo'
     return addRequiredID(PassClass::ID);

我的明显问题:为什么它抱怨我正在尝试无效转换?我在那些片段中没有看到这样的尝试。

FunctionPass(&id) 替换为 FunctionPass(id)

char & pidpid 声明为对 char 的引用,而您传递的 &id 是指向 char.

的指针