我很确定这个问题是重复的,但我的代码在这里有所不同,以下是我的代码.它失败了"未定义的符号"错误,不确定丢失了什么.
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 ++ 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) 我试图编写自己的简单倒计时迭代器,我实现了一个__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继承案例,但我在理解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)