Help me insert entries into a C++ std map
map<int, BasicBlock*> basicBlocks; // in my header file
basicBlocks.insert(std::pair<int, BasicBlock*>(pc, bb));
where pc is an integer and bb is a BasicBlock: (earlier)
BasicBlock *bb = new BasicBlock(pc);
that is from earlier in the code.
开发者_StackOverflow中文版I get this error: error C2664: 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'BasicBlock *' to '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
}
You have the line:
basicBlocks.insert(new BasicBlock(pc));
This is trying to insert a BasicBlock*
into the map, not a pair. Did you mean:
basicBlocks.insert(std::make_pair(pc, new BasicBlock(pc)) );
You can also just write:
basicBlocks[pc] = bb;
However, whatever method you use, you need to be sure that you only have one entry for each value of pc. If you need multiple entries, you multimap<>.
精彩评论