当将freeSeats设置为0时,我的代码仍然说一个人在他/她的汽车中有可用座位。
我创建了两个类。一辆车,一个人。Car类具有查看汽车中是否有空位的功能。一个人对象可以有一辆汽车。当检查人员是否有可用座位时,即使我输入“ 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;
}
Run Code Online (Sandbox Code Playgroud)
我懂了
姓名:Aleksander电子邮件:aleks@aleks.com有免费座位:是
姓名:萨拉电子邮件:sara@sara.com有免费座位:是
姓名:Daniel电子邮件:daniel@daniel.com有免费座位:是
姓名:克里斯电子邮件:chris@chris.com有免费座位:否
但我希望丹尼尔的自由座位为“否”
这里有一个错字:
Car::Car(unsigned int freeSeas) :
freeSeats{ freeSeats }
{}
Run Code Online (Sandbox Code Playgroud)
您写的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 }
^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我不必了解所有内容,但是它告诉我两件事:
它使我更仔细地了解了此构造函数,然后您可以看到错字。