为什么我的代码应该说“否”?

时间:2019-10-15 12:01:52

标签: c++ class boolean

当将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;
}

我明白了

名称:Aleksander 电子邮件:aleks@aleks.com 有免费座位:是

名称:Sara 电子邮件:sara@sara.com 有免费座位:是

名称:Daniel 电子邮件:daniel@daniel.com 有免费座位:是

姓名:克里斯 电子邮件:chris@chris.com 有免费座位:否

但是我希望丹尼尔的自由座位为“否”

2 个答案:

答案 0 :(得分:9)

这里有一个错字:

Car::Car(unsigned int freeSeas) :
    freeSeats{ freeSeats }
    {}

您写的是freeSeas而不是freeSeats。因此,freeSeas参数未使用,而freeSeats{ freeSeats }则无济于事,因为freeSeats引用的是成员变量,而不是参数。

答案 1 :(得分:6)

启用编译器警告后,

调试会更容易。编译器是您的朋友,如果您愿意听,它将为您提供极大的帮助。

例如,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. 使用未初始化的值初始化变量(为什么?)

这让我更仔细地看了看这个构造函数,然后您可以看到错字。