Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 抽象基类与公共继承_C++_Inheritance - Fatal编程技术网

C++ 抽象基类与公共继承

C++ 抽象基类与公共继承,c++,inheritance,C++,Inheritance,我希望你们这些优秀的绅士能帮我澄清一些事情 假设我有以下代码: class someABC{ virtual void foo() = 0; } 以及从中继承的另一个类: class inheritFromABS : public someABC { inheritFromABS(); void foo(); } 另一个类也是从someABC继承的: class alsoInheritFromABS : public someABC { alsoInheritFromABS(

我希望你们这些优秀的绅士能帮我澄清一些事情

假设我有以下代码:

class someABC{
  virtual void foo() = 0;
}
以及从中继承的另一个类:

class inheritFromABS : public someABC
{
  inheritFromABS();
  void foo();
}
另一个类也是从someABC继承的:

class alsoInheritFromABS : public someABC
{
  alsoInheritFromABS();
  void foo();
}
现在我有一个someABC类型的对象列表,如下所示:

  list<someABC *> myList;
  inheritFromABS *first = new inheritFromABS();
  alsoInheritFromABS *second = new alsoInheritFromABS();
  myList.push_back(first);
  myList.push_back(second);
列出myList;
inheritFromABS*first=新的inheritFromABS();
alsoInheritFromABS*秒=新的alsoInheritFromABS();
我的列表。向后推(第一);
我的列表。向后推(秒);
我想遍历myList,并对每个myList调用foo()。这是否可行?它编译时没有错误或警告,但在我尝试调用foo()时,我遇到了分段错误

我是否需要有类型为inheritFromABS和alsoInheritFromABS的对象列表才能调用foo();每个人

编辑:这是我的实际代码,它因分段错误而失败:

llvm::Value *decafStmtList::Codegen() {
for(list<decafAST*>::iterator i = stmts.begin(); i != stmts.end(); i++)
{
    decafAST *tmp = *i;    
    tmp->Codegen();
}
return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
}
llvm::Value*decafStmtList::Codegen(){
对于(列表::迭代器i=stmts.begin();i!=stmts.end();i++)
{
十进位*tmp=*i;
tmp->Codegen();
}
返回常量::getNullValue(类型::getDoubleTy(getGlobalContext());
}
因此,STMT包含VariableExprAST类型的一个对象,我希望它调用VariableExprAST的codegen,代码如下:

llvm::Value *VariableExprAST::Codegen() {
    cout << "I am here" << endl;
    return 0;//FIXME: im broken
}
llvm::Value*VariableExprAST::Codegen(){

cout您尝试的内容看起来是正确的,但您需要在列表中存储指针。目前不清楚您当前的代码将如何编译,但您可能打算这样做:

list<someABC *> myList;
inheritFromABS* first = new inheritFromABS();
alsoInheritFromABS* second = new alsoInheritFromABS();
myList.push_back(first);
myList.push_back(second);

你所描述的应该是可行的,所以你在其他地方肯定有问题。不,这很简单,应该没有任何问题。如果你给我们看你的代码,我们可以告诉你想要的是错误的。你把对象放在指针列表中,你有指向someABC的指针,而不是“someABC类型的对象列表”当您到达
decafStmtList::Codegen()
时,看起来
stmts
状态不好,因此尝试取消对
stmts.begin()的引用
崩溃。在到达那里之前,您必须查看
stmts
发生了什么。如果类
decafStmtList
没有有效的复制构造函数和/或赋值运算符,则另一种可能是复制
decafStmtList
对象。@user1039063这是在不发布真实代码时发生的情况,您会得到一个回答的问题与您想要的不同。请发布一些真实代码。@user1039063当John说“真实代码”时,他指的是一些复制问题的小示例,而不是数百行几乎不相关的代码:-)嗯,是的,我应该自己做这个示例!谢谢你们,我的问题在别处!
#include <iostream>
#include <list>

struct Animal
{
  virtual void talk() const =0;
  virtual ~Animal() {}
};

struct Dog : Animal
{
  void talk() const { std::cout << "Woof woof!\n"; }
};

struct Cow : Animal
{
  void talk() const { std::cout << "Moooo!\n"; }
};

int main()
{
  std::list<Animal*> animals;
  animals.push_back(new Dog);
  animals.push_back(new Cow); 
  animals.push_back(new Dog);
  animals.push_back(new Cow);
  animals.push_back(new Dog);
  animals.push_back(new Cow);

  for (std::list<Animal*>::const_iterator it = animals.begin();
       it != animals.end();
       ++it)
  {
    (*it)->talk();
  }
}