为什么我的代码说"Yes"什么时候应该说"No"?

Why does my code say "Yes" when it should say "No"?

本文关键字:应该说 No 什么时候 Yes 代码 为什么 我的      更新时间:2023-10-16

当将freeSeats设置为0时,我的代码仍然说一个人在他/她的车里有可用的座位。

我创建了两个类。一个用于汽车,一个用于人。汽车类有一个功能,可以查看车内是否有空位。一个人的物体可以有一辆车。当检查此人是否有可用座位时,即使我输入"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;
}

我得到

姓名:亚历山大 电子邮件: aleks@aleks.com 有空席:是

姓名:萨拉 电子邮件: sara@sara.com 有空席:是

姓名:丹尼尔 电子邮件: daniel@daniel.com 有空席:是

姓名:克里斯 电子邮件: 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 }
^~~~~~~~~

我不必理解所有内容,但它告诉我两件事:

  1. 有未使用的参数(为什么?它用于初始化...(
  2. 变量使用未初始化的值初始化(为什么?

它让我仔细观察这个构造函数,然后你可以看到拼写错误。

相关文章: