我收到此错误消息,代码如下:
class Money {
public:
Money(float amount, int moneyType);
string asString(bool shortVersion=true);
private:
float amount;
int moneyType;
};
Run Code Online (Sandbox Code Playgroud)
首先,我认为默认参数不允许作为C++中的第一个参数,但允许使用.
我使用easy_install在Windows上安装了virtualenv和virtualenvwrapper.但是mkvirtualenv缺少了.我试图在我的机器上搜索,但我找不到它.我不知道如何解决它.你有什么主意吗?
>>> from datetime import datetime
>>> t1 = datetime.now()
>>> t2 = datetime.now()
>>> delta = t2 - t1
>>> delta.seconds
7
>>> delta.microseconds
631000
Run Code Online (Sandbox Code Playgroud)
有没有办法让它成为7.631000?我可以使用时间模块,但我还需要将t1和t2变量作为DateTime对象.因此,如果有一个简单的方法来实现datettime,那就太棒了.否则它看起来很难看:
t1 = datetime.now()
_t1 = time.time()
t2 = datetime.now()
diff = time.time() - _t1
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
class Soldier {
public:
Soldier(const string &name, const Gun &gun);
string getName();
private:
Gun gun;
string name;
};
class Gun {
public:
void fire();
void load(int bullets);
int getBullets();
private:
int bullets;
}
Run Code Online (Sandbox Code Playgroud)
我需要调用Gun的所有成员函数而不是Soldier对象.就像是:
soldier.gun.fire();
Run Code Online (Sandbox Code Playgroud)
要么
soldier.getGun().load(15);
Run Code Online (Sandbox Code Playgroud)
那么哪一个是更好的设计?将枪对象隐藏为私有成员并使用getGun()函数访问它.或者让它成为公共会员?或者我可以封装所有这些函数会使实现更难:
soldier.loadGun(15); // calls Gun.load()
soldier.fire(); // calls Gun.fire()
Run Code Online (Sandbox Code Playgroud)
那么你觉得哪一个最好?
我刚开始使用Sphinx工具为我的代码生成文档.但我有点困惑,因为它并不像我预期的那么容易.我使用以下方法创建Sphinx文档:
sphinx-quickstart
Run Code Online (Sandbox Code Playgroud)
然后我将我的*.rst文件创建到"source"文件夹中.好像我需要为我想要为其创建文档的每个模块创建一个*.rst文件.对于test.py,我创建了test.rst.在test.rst中,我有:
.. automodule:: test
:members:
:show-inheritance:
Run Code Online (Sandbox Code Playgroud)
然后在test.py里面,我有:
"""
.. module:: test
:platform: Unix, Windows
:synopsis: A useful module indeed.
"""
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法生成文档:
sphinx-build -b html source/ build/
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作,但问题是它并不像我预期的那么容易.我想必须有一个更简单的方法来跳过其中的一些步骤.我想知道是否有任何方法可以为包内的所有模块生成文档,而不是为每个模块生成*.rst文件.
谢谢.
我需要编写一个算法(我不能使用任何第三方库,因为这是一个赋值)来划分(整数除法,浮动部分并不重要)非常大的数字,如100 - 1000位数.我找到了http://en.wikipedia.org/wiki/Fourier_division算法,但我不知道这是不是正确的方法.你有什么建议吗?
1) check divisior < dividend, otherwise it's zero (because it will be an int division)
2) start from the left
3) get equal portion of digits from the dividend
4) if it's divisor portion is still bigger, increment digits of dividend portion by 1
5) multiply divisor by 1-9 through the loop
6) when it exceeds the dividend portion, previous multiplier is the answer
7) repeat steps 3 to 5 until reaching to the end
Run Code Online (Sandbox Code Playgroud) 这是从函数返回对象的正确方法吗?
Car getCar(string model, int year) {
Car c(model, year);
return c;
}
void displayCar(Car &car) {
cout << car.getModel() << ", " << car.getYear() << endl;
}
displayCar(getCar("Honda", 1999));
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,"收取临时地址".我应该这样使用:
Car &getCar(string model, int year) {
Car c(model, year);
return c;
}
Run Code Online (Sandbox Code Playgroud) 我在控制聚焦方面遇到了一些问题.我的界面定义如下:
源(RadioGroup /可选)目标(EditText)数量(EditText)传输(按钮)
我正在将"源"的可见性更改为我的代码.当我没有显示它的时候,焦点会自动转到"数量",我希望它是"目的地".我不知道我错过了什么.我怀疑它可能是一个Android bug,我不知道.有人知道如何解决这个问题吗?
谢谢
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup android:id="@+id/source"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<RadioButton android:id="@+id/default"
android:checked="false"
android:text="@string/default"/>
</RadioGroup>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="@string/destination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#ffffff"
android:paddingRight="10sp"
android:layout_weight="1"/>
<EditText android:id="@+id/destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"><requestFocus/></EditText>
</TableRow>
<TableRow>
<TextView
android:text="@string/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#ffffff"
android:paddingRight="10sp"
android:layout_weight="1"/>
<EditText android:id="@+id/quantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"/>
</TableRow>
</TableLayout>
<Button android:id="@+id/transfer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/transfer"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 在C++中,可以说:
for (int i = 0; i < 100 && !found; i++) {
if (items[i] == "the one I'm looking for")
found = true;
}
Run Code Online (Sandbox Code Playgroud)
所以你不需要使用"break"语句.
在Python中,我想你需要写:
found = False
for item in items:
if item == "the one I'm looking for"
found = True
break
Run Code Online (Sandbox Code Playgroud)
我知道我可以编写一个具有相同代码的生成器,所以我可以隐藏这个破坏的东西.但我想知道是否有任何其他方法可以实现相同的功能(具有相同的性能),而无需使用额外的变量或while循环.
我知道我们可以说:
found = "the one I'm looking for" in items
Run Code Online (Sandbox Code Playgroud)
我只是想了解是否可以在for循环中使用多个条件.
谢谢.
这是我的装饰代码.我出于某种原因得到UnboundLocalError但我找不到它.
>>> def validate(schema=None):
def wrap(f):
def _f(*args, **kwargs):
if not schema:
schema = f.__name__
print schema
return f()
return _f
return wrap
>>> @validate()
def some_function():
print 'some function'
>>> some_function()
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
some_function()
File "<pyshell#22>", line 4, in _f
if not schema:
UnboundLocalError: local variable 'schema' referenced before assignment
>>>
Run Code Online (Sandbox Code Playgroud)
所以,我想也许最好在这里发帖.我可能会遗漏一些东西.
谢谢.