在数据结构中学习C++。
这很简单,但我不知道我做错了什么 这是我的代码:Main.cpp在数据结构中学习C++。,c++,structure,C++,Structure,这很简单,但我不知道我做错了什么 这是我的代码:Main.cpp #include <cstdlib> #include <iostream> #include "throttle.h" using namespace std; int main(int argc, char** argv) { throttle example; example.shut_off(); int in = 2; example.shift(4);
#include <cstdlib>
#include <iostream>
#include "throttle.h"
using namespace std;
int main(int argc, char** argv) {
throttle example;
example.shut_off();
int in = 2;
example.shift(4);
cout << "At position 2, the flow is " << example.flow() << endl;
}
和throttle.cpp
#include "throttle.h"
/*
throttle::throttle() {
}
throttle::throttle(const throttle& orig) {
}
throttle::~throttle() {
}
*/
//Pre:
//POST: Returns double position / total positions
double throttle::flow() const {
return double (position / 6);
}
bool throttle::is_on() {
return (flow() > 0);
}
void throttle::shift(int amount) {
position += amount;
if (amount > 6)
position = 6;
else if (amount < 0)
position = 0;
}
void throttle::shut_off() {
position = 0;
}
我的问题是不清楚C++,但在我的主要中,为什么我的移位方法不起作用?函数可能是正确的词,而不是方法
我的逻辑是:
调用关闭方法将位置设置为0
调用shift方法将位置设置为4。没有返回任何内容,只是将位置设置为4。但它没有这样做
<> P>任何一个流利的C++,你也可以指出任何其他的错误编码实践。我知道我没有在实现中编写前置和后置条件当你说“不做”时,你可能是说你的流为0?但这是意料之中的,因为你的位置被6除,而且位置是整数,还有6,所以你得到的是整数0。如果您想要双倍编号,则需要在除法前将位置转换为双倍:
(double) position / 6
另一方面,您的代码将除法整数0的结果转换为双0。当您说“不做”时,它实际上做了什么?对不起,应该将输出放在第2位,但输出是:在第2位,流是0;是的,我知道了。看看我的答案。
(double) position / 6