[英]Why does my code say “Yes” when it should say “No”?
当将 freeSeats 设置为 0 时,我的代码仍然说一个人在他/她的车上有可用的座位。
我创建了两个类。 一个用于汽车,一个用于人。 汽车 class 有一个 function 看车内是否有空位。 一个人-对象可以有一辆汽车。 在检查此人是否有可用座位时,即使我输入“0”,我的代码也会响应“是”。 为什么?
#pragma once
#include <iostream>
//Here is class Car declaration
class Car {
private:
unsigned int freeSeats;
public:
bool hasFreeSeats() const;
void reserveFreeSeat();
Car( unsigned int freeSeats);
};
//Here is function definition
#include "Car.h"
bool Car::hasFreeSeats() const {
if (freeSeats > 0)
return true;
return false;
}
void Car::reserveFreeSeat() {
--freeSeats;
}
Car::Car(unsigned int freeSeas) :
freeSeats{ freeSeats }
{
}
//Here is class Person declaration
class Person {
private:
std::string name;
std::string email;
Car *car; //pointer to a car
public:
Person(std::string name, std::string email, Car *car = nullptr);
std::string getName() const;
std::string getEmail() const;
void setEmail();
bool hasAvalibaleSeats() const;
friend std::ostream& operator << (std::ostream& os, const Person& p);
};
//Here is function definition
Person::Person(std::string name, std::string email, Car *car) :
name{ name }, email{ email }, car{ car }
{
}
std::string Person::getName() const {
return name;
}
std::string Person::getEmail() const {
return email;
}
void Person::setEmail() {
std::string newEmail;
std::cout << "What is the e-mail adress?";
std::cin >> newEmail;
email = newEmail;
std::cout << "E-mail has been set." << std::endl;
}
bool Person::hasAvalibaleSeats() const {
if (car != nullptr) { //check if there is a car
return car->hasFreeSeats();
}
return false;
}
std::ostream& operator << (std::ostream& os, const Person& p) {
std::string seats = "No";
if (p.hasAvalibaleSeats())
seats = "Yes";
return os << "Name: " << p.name << "\nE-mail: " << p.email << "\nHas free seats: " << seats << std::endl;
}
//From main im calling
#include "Car.h"
#include "Person.h"
int main() {
Car ferrari{ 2 };
Car bugatti{ 3 };
Car jeep{0};
Person one{ "Aleksander","aleks@aleks.com", &ferrari };
Person two{ "Sara","sara@sara.com", &bugatti };
Person three{ "Daniel", "daniel@daniel.com", &jeep };
Person four{ "Chris", "chris@chris.com" };
std::cout << one << std::endl;
std::cout << two << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
system("pause");
return 0;
}
我明白了
姓名:Aleksander 电子邮件:aleks@aleks.com 有空位:是
姓名:Sara 电子邮件:sara@sara.com 有空位:是
姓名:Daniel 电子邮件:daniel@daniel.com 有空位:是
姓名:Chris 电子邮件:chris@chris.com 有空位:否
但我希望丹尼尔有空位说“不”
这里有一个错字:
Car::Car(unsigned int freeSeas) :
freeSeats{ freeSeats }
{}
你写了freeSeas
而不是freeSeats
。 因此, freeSeas
参数未使用,并且freeSeats{ freeSeats }
什么也不做,因为freeSeats
的是成员变量,而不是参数。
当您启用编译器警告时,调试会更容易。 编译器是你的朋友,如果你愿意的话,它会给你很大的帮助。
例如, gcc
在编译代码时给了我以下警告:
prog.cc: In constructor 'Car::Car(unsigned int)':
prog.cc:37:23: warning: unused parameter 'freeSeas' [-Wunused-parameter]
Car::Car(unsigned int freeSeas) :
~~~~~~~~~~~~~^~~~~~~~
prog.cc: In constructor 'Car::Car(unsigned int)':
prog.cc:38:16: warning: '*<unknown>.Car::freeSeats' is used uninitialized in this function [-Wuninitialized]
freeSeats{ freeSeats }
^~~~~~~~~
我不必了解所有内容,但它告诉我两件事:
它让我仔细观察了这个构造函数,然后你就可以看到错字了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.