我正在将子对象解析为一个方法,但我的子数据丢失了。请告诉如何在不丢失数据的情况下解析对象。
class A
{
int size;
std::string name;
public:
A(const std::string &name, int &size){}
virtual B *C();
}
function D()
{
int size = 10;
std::string name = "name";
return new A(name , size);
}
B *A::C(){
\\here I need name and size
}
现在写它给出的大小值是 0 而不是 10 并且它给出了分段错误的名称
感谢4提前帮助
更新 1 我的代码摘要
class PrototypeAST
{
int size;
std::string FnName;
std::vector<std::string> ArgNames;
public:
PrototypeAST(const std::string &fnName, const std::vector<std::string> &argNames, int &size)
: FnName(fnName), ArgNames(argNames) {}
Function *Codegen();
void CreateArgumentAllocas(Function *F);
};
static PrototypeAST *ParsePrototype() {
int size;
std::string FnName = IdentifierStr;
getNextToken();//eat id1
std::vector<std::string> ArgNames;
if(CurTok != ')' )
{
getNextToken(); //eat int
ArgNames.push_back(IdentifierStr);
getNextToken();// eat id
while (CurTok == ',')
{
getNextToken(); //eat ,
getNextToken(); //eat int
ArgNames.push_back(IdentifierStr);
getNextToken();// eat id
}
}
// success.
getNextToken(); // eat ')'.
size = ArgNames.size();
return new PrototypeAST(FnName, ArgNames, size);
}
Function *PrototypeAST::Codegen() {
printf("\nI am in prototypeAST function\n");
// Make the function type: double(double,double) etc.
std::vector<Type*> Doubles(size,
Type::getInt1Ty(getGlobalContext()));
printf("\nI am in prototypeAST function's 1\n");
FunctionType *FT;
if(isFunInt)
FT = FunctionType::get(Type::getInt1Ty(getGlobalContext()),
Doubles, false);
else if(isFunVoid)
FT = FunctionType::get(Type::getInt1Ty(getGlobalContext()),
Doubles, false);
printf("\nI am in prototypeAST function's 2\n");
Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, TheModule);
printf("\nI am in prototypeAST function's 3\n");
// If F conflicted, there was already something named 'Name'. If it has a
// body, don't allow redefinition or reextern.
if (F->getName() != FnName) {
// Delete the one we just made and get the existing one.
F->eraseFromParent();
F = TheModule->getFunction(FnName);
}
// Set names for all arguments.
unsigned Idx = 0;
for (Function::arg_iterator AI = F->arg_begin(); Idx != ArgNames.size();
++AI, ++Idx) {
AI->setName(ArgNames[Idx]);
}
printf("\nI am in prototypeAST function\n");
return F;
}