【问题标题】:How to pass child object in the method如何在方法中传递子对象
【发布时间】:2013-10-15 11:06:51
【问题描述】:

我正在将子对象解析为方法,但我的子数据丢失了。请告诉如何在不丢失数据的情况下解析对象。

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; 
}

【问题讨论】:

  • 你能发布一些更接近真实 C++ 代码的东西吗?
  • 您好,欢迎来到 stackoverflow.com。请阅读the Stack Overflow question checklist。您可能还想了解SSCCE 是什么。
  • 如果你真的有一个空的构造函数,我建议你从那里开始寻找问题。请记住,赋予事物相同的名称并不会使它们成为相同的事物。
  • 我的代码只是这样,但我不认为空的构造函数是问题所在。但如果是的话,你能建议类似这段代码的东西吗?
  • @juanchopanza 我添加了更新,这是真正的 C++ 代码的一部分

标签: c++ parameter-passing


【解决方案1】:

正如其他人在 cmets 中指出的那样,您应该查看空构造函数。您没有在构造函数中设置数据成员的值。这就是错误的原因。

PS:请熟悉 Stack Overflow 问题清单。快乐学习。

【讨论】:

  • 这里还有一个问题function D()实际上是function A *D()会返回什么它不会反映在我的构造函数类中..???
  • 我正在尝试做的事情是在llvm tutorial 以同样的方式完成。但我得到了错误。
【解决方案2】:

我有你想做的事。方法如下。

空的构造函数造成了问题。您可以通过以下方式使用函数返回的值初始化您的参数。

class A
{
  int Size;    
  std::string Name;
public:    
  A(const std::string &name, int &size):Name(name), Size(size) {}    
  virtual B *C();
}

A *D()
{
  int size = 10;
  std::string name = "name";
  return new A(name , size);
}
B *A::C(){
  \\here I need name and size
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 2012-10-23
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多