我有一个返回bool的函数:
bool restart()
{
std::string answer;
bool answered = false;
while(answered == false)
{
cout << endl << endl << "Do you want to play again? y/n : ";
cin >> answer;
if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
{return true;}
if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
{return false;}
}
}
Run Code Online (Sandbox Code Playgroud)
当我用它来调用它时:
cout << restart();
Run Code Online (Sandbox Code Playgroud)
我得到输出:
Do you want to play again? y/n …Run Code Online (Sandbox Code Playgroud) 如果我有一个包含setup.py文件的 python 项目,我可以运行pip install --user -e .. 这将安装安装文件中列出的所有要求,并将当前项目添加到我的 pip 列表中:
$ pip show project-name
Name: project-name
Version: 1.0.0
Summary: None
Location: /path-to-project/
Requires: matplotlib, numpy, scipy, ...
Required-by:
Run Code Online (Sandbox Code Playgroud)
默认情况下,这会添加\path-to-project\到用户 PYTHONPATH 中,还是您必须手动执行此操作,以便您可以import project-name从系统上的任何位置导入代码?
我正在学习c ++中的类和对象,并尝试使用以下代码来测试我是否理解它:
#include <iostream>
using namespace std;
class class1
{
public:
void write(int x)
{
dataObject.var = x;
}
};
class class2
{
public:
void read()
{
std::cout << dataObject.var;
}
};
class data
{
public:
int var;
data()
{
var = 1;
}
};
int main()
{
data dataObject;
class1 object1;
class2 object2;
object2.read(data dataObject);
object1.write(2);
object2.read(data dataObject);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是两个对象都用于修改和使用第三个成员,但我得到以下错误:
In member function 'void class1::write(int)':
line 10: error: 'dataObject' was not declared in this scope
In member …Run Code Online (Sandbox Code Playgroud) 我刚刚学习了运算符重载,我想尝试创建一个表示复数的对象.我写了以下内容:
#include <iostream>
using namespace std;
class complexNumber
{
public:
double re, im;
complexNumber(){}
complexNumber(double a, double b)
{
re = a;
im = b;
}
complexNumber operator+ (complexNumber b)
{
complexNumber c;
c.re = re + b.re;
c.im = im + b.im;
}
complexNumber operator- (complexNumber b)
{
complexNumber c;
c.re = re - b.re;
c.im = im - b.im;
}
};
int main()
{
complexNumber a(1,2);
complexNumber b(4,6);
complexNumber c;
complexNumber d;
c = a + b;
d …Run Code Online (Sandbox Code Playgroud)