我是c ++的初学者,因此为这个愚蠢的问题道歉.我在这里发帖是因为我在stackoverflow上找不到类似的答案.
我正在逐步完成C++中的异常,而且我正在尝试使用自定义异常,我有这个代码
class MyException: public std::exception{
public:
virtual const char* what() const throw() {
return "something bad happened";
}
};
// class that throws above exception
class canGoWrong {
public:
canGoWrong(){
throw MyException();
}
};
Run Code Online (Sandbox Code Playgroud)
上面的代码由老师展示.构造函数刚刚实现了基类中定义的虚函数exception.我到那儿.
现在,当我尝试使用不同版本进行练习时,我尝试使用自定义函数而不是重新定义虚拟(因为c ++没有严格执行接口的概念,如果我在这里错了,请纠正我.)
我把它写成了
class my_custom_shit_exception: public std::exception {
public:
const char* show() { // I omitted the const throw() here
return "This is an error encountered\n";
}
};
class myclass {
public:
myclass() {
throw my_custom_shit_exception();
}
};
Run Code Online (Sandbox Code Playgroud)
总而言之,我没有发现两种方式的行为差异
public:
const …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 if 条件在 bash 中查找特定进程的数量
if ps auwx | grep -v grep | grep -ic python -le 2; then echo error; else echo no_error; fi
Run Code Online (Sandbox Code Playgroud)
我得到的输出为
grep: python: No such file or directory
Run Code Online (Sandbox Code Playgroud)
没有错误
如果我使用管道,单行似乎会中断,如果我省略管道,则不会抛出任何错误,如果我使用绝对路径来 grep 也没有关系。没有管道,我无法获得所需的结果。我在这里做错了什么?我可以在脚本文件中完成此操作,方法是将其分解为变量然后进行比较,但我将其用作学习 bash 的练习。任何帮助是极大的赞赏。
您好,社区,我正在学习OOPS概念与python作为我的课程的一部分.我在python中遇到多重继承问题.以下是我的代码:
#!/usr/bin/env python
class Base1(object):
def __init__(self):
self.base1var = "this is base1"
class Base2(object):
def __init__(self):
self.base2var = "this is base2"
class MainClass(Base1, Base2):
def __init__(self):
super(MainClass, self).__init__()
if __name__ == "__main__":
a = MainClass()
print a.base1var
print a.base2var
Run Code Online (Sandbox Code Playgroud)
并且在运行时,我收到以下错误
print a.base2var
AttributeError: 'MainClass' object has no attribute 'base2var'
Run Code Online (Sandbox Code Playgroud)
如果我交换继承的类的顺序,则错误中的变量名会相应地更改.
super()当我想调用两个继承类的构造函数时,我是否使用了错误?
如何才能正确地从多个基类继承并使用主类中的变量和方法而不会出现此错误?
谢谢.
如何使用defaultdict创建列表字典?我收到以下错误.
>>> from collections import defaultdict
>>> a=defaultdict()
>>> a["testkey"]=None
>>> a
defaultdict(None, {'testkey': None})
>>> a["testkey"]["list"]=[]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object does not support item assignment
Run Code Online (Sandbox Code Playgroud) 为蹩脚的问题道歉.我正在使用Intellij Clion Student许可版本用于我的C++课程.作为实现UnsortedList类的一部分,我们必须编写一个方法isInTheList来查看数组中是否存在元素.类实现如下
bool UnsortedList::isInTheList(float item) {
for (int i = 0; i < length; i++) {
if (data[i] == item) {
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,ide data[i] == item用弹出的说法显示了一个彩色标记
Statement can be simplified less... (Ctrl+F1)
This inspection finds the part of the code that can be simplified, e.g. constant conditions, identical if branches, pointless boolean expressions, etc.
Run Code Online (Sandbox Code Playgroud)
对于先前检查列表是否为空的方法,我使用以下简化形式而不是if-else语句.
bool UnsortedList::isEmpty() {
return (length == 0);
}
Run Code Online (Sandbox Code Playgroud)
但是,现在涉及迭代,我无法在前者中提出简化的陈述.任何帮助深表感谢.谢谢.
c++ ×2
if-statement ×2
python ×2
bash ×1
class ×1
defaultdict ×1
dictionary ×1
exception ×1
inheritance ×1
linux ×1
oop ×1
pipe ×1
shell ×1
simplify ×1
throw ×1