c++ - 帮助我将条目插入到 C++ std 映射中

标签 c++ dictionary

map<int, BasicBlock*> basicBlocks; // in my header file
basicBlocks.insert(std::pair<int, BasicBlock*>(pc, bb)); 

其中 pc 是一个整数,bb 是一个 BasicBlock:(之前)

BasicBlock *bb = new BasicBlock(pc); 

那是从前面的代码。

我收到此错误:错误 C2664:“std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)”:无法将参数 1 从“BasicBlock *”转换为“const std::pair<_Ty1,_Ty2> &'

Why would it even need to convert the parameter? 



    void ConstantPoolParser::createBasicBlocks(Method* method)
    {
    cout << "Creating basic block's in method: " << method->getName() << endl;
    char* bytecode = method->getBytecode();
    int bytecodeLength = method->getBytecodeLength();
    int pc = 0;
    basicBlocks.insert(new BasicBlock(pc));

    for(pc = 0; pc < bytecodeLength; pc++)
    {
        int opcode = bytecode[pc] & 0xFF;
        switch(opcode)
        {
            case IF_ICMPEQ: // 159 (0x9f) eq
            case IF_ICMPNE: // 160 (0xa0) ne
            case IF_ICMPLT: // 161 (0xa1) lt
            case IF_ICMPGE: // 162 (0xa2) ge
            case IF_ICMPGT: // 163 (0xa3) gt
            case IF_ICMPLE: // 164 (0xa4) le
            case GOTO: // 167 (0xa7) goto
            {
                int branchbyte1 = bytecode[pc+1] & 0xFF;
                int branchbyte2 = bytecode[pc+2] & 0xFF;
                int branchTarget =  branchbyte1 << 8 | branchbyte2 + pc; //(branchbyte1 << 8) | branchbyte2
                cout << "we got into a branch case! " << opcode << " Branch target: " << branchTarget << endl;

                basicBlocks.insert( std::pair<int, BasicBlock*>(pc, new BasicBlock(pc)) );
                basicBlocks.insert( std::pair<int, BasicBlock*>(branchTarget, new BasicBlock(branchTarget)) );

                pc+=2; // loop will add 1 to pc
                break;
            }
        }//end switch
    }// end for
}

最佳答案

你有这条线:

basicBlocks.insert(new BasicBlock(pc));

这是试图将 BasicBlock* 插入到 map 中,而不是一对。你的意思是:

basicBlocks.insert(std::make_pair(pc, new BasicBlock(pc)) );

关于c++ - 帮助我将条目插入到 C++ std 映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777854/

相关文章:

python按值然后按键对字典项进行排序

python-3.x - Python - 如何将函数应用于字典

emacs - Common Lisp 有一些功能。我怎样才能在 elisp 中写一些或任何东西?

c++ - 使用 Abaqus ODB C++ API 访问部件

c++ - Sizeof 指向数组的指针

c++ - 提升精神 2.x : how to deal with keywords and identifiers?

javascript - 如何在 OpenLayers 3 中运行经纬网标签?

c++ - 让本地符号走向全局

c++ - 使用 FFMPEG 读取每帧时间码?

python - 字典内的字符串格式?