小编Apa*_*nti的帖子

缺少的vtable通常意味着第一个非内联虚拟成员函数没有定义

我很确定这个问题是重复的,但我的代码在这里有所不同,以下是我的代码.它失败了"未定义的符号"错误,不确定丢失了什么.

class Parent {
   public :
     virtual int func () = 0;
     virtual ~Parent();

 };


 class Child : public Parent {
     public :

     int data;
     Child (int k) {
        data = k;
      }
    int func() {   // virtual function
       cout<<"Returning square of 10\n";
        return 10*10;
    }

    void Display () {
    cout<<data<<"\n";

 }

 ~ Child() {

    cout<<"Overridden Parents Destructor \n";

 }
};



int main() {
  Child a(10);
 a.Display();

 }
Run Code Online (Sandbox Code Playgroud)

以下是编译时的O/P.

Undefined symbols for architecture x86_64:
  "Parent::~Parent()", referenced from:
      Child::~Child() in inher-4b1311.o …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-destructor

9
推荐指数
2
解决办法
2万
查看次数

在c ++ 11中初始化字符串列表

我试图使用以下代码初始化c ++ 11中的字符串列表,并且由于各种原因而失败.错误说我需要使用构造函数来初始化列表,我应该使用类似的东西list<string> s = new list<string> [size]吗?我在这里错过了什么?

#include<string>
#include<list>
#include<iostream>
using namespace std;

int main() {
      string s = "Mark";
      list<string> l  {"name of the guy"," is Mark"};
      cout<<s<<endl;
      int size = sizeof(l)/sizeof(l[0]);
      for (int i=0;i<size;i++) {
             cout<<l[i]<<endl;
      }
      return 0;
 }
Run Code Online (Sandbox Code Playgroud)

I/O是

 strtest.cpp:8:47: error: in C++98 ‘l’ must be initialized by constructor, not 
 by ‘{...}’
 list<string> l  {"name of the guy"," is Mark"};
Run Code Online (Sandbox Code Playgroud)

c++ list

4
推荐指数
2
解决办法
5067
查看次数

一个简单的Python迭代器进入无限循环

我试图编写自己的简单倒计时迭代器,我实现了一个__iter__()函数和相应的函数__next__()来支持迭代器。我在函数yield内部使用了一个函数__next__(),以便每次迭代对象时返回一个新值。与using 语句相比,当我使用 时yield,代码会进入无限循环return。以下是我的代码:

class MyIterator():
    def __init__(self,value):
        self.value = value
    def __iter__(self):
        return self
    def __next__(self):
        print("In the next function")
        if self.value > 0:
            yield self.value
            self.value -= 1
        else:
            raise StopIteration("Failed to proceed to the next step")



if __name__ == '__main__':
    myIt = MyIterator(10)
    for i in myIt:
        print(i)
Run Code Online (Sandbox Code Playgroud)

其O/P如下:

  <generator object __next__ at 0x101181990>
  <generator object __next__ at 0x1011818e0>
  <generator object __next__ at 0x101181990>
  <generator object …
Run Code Online (Sandbox Code Playgroud)

python iterator yield generator

3
推荐指数
1
解决办法
2269
查看次数

从基类扩展方法时的TypeError

我试图测试一个简单的Python继承案例,但我在理解Python解释器吐出的错误时遇到了问题.

class Mainclass(object):
    """
     Class to test the inheritance
    """
    def __init__(self,somevalue):
        self.somevalue = somevalue
    def display(self):
        print(self.somevalue)


class Inherited(Mainclass):
    """
    Inherited class from the Main Class
    """
    def display(self):
        print("**********")
        Mainclass.display()
        print("**********")

c = Inherited(100)
c.display()
Run Code Online (Sandbox Code Playgroud)

我只是试图在Inherited类中显示的输出中添加星号,那么为什么它会因以下错误而失败?

Traceback (most recent call last):
line 21, in <module>
c.display()
line 17, in display
Mainclass.display()
TypeError: display() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)

python methods inheritance

0
推荐指数
1
解决办法
38
查看次数