【问题标题】:Why does my code say "Yes" when it should say "No"?为什么我的代码应该说“否”时却说“是”?
【发布时间】:2020-02-12 02:35:50
【问题描述】:

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

我明白了

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

姓名:萨拉 邮箱:sara@sara.com 有空位:是

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

姓名:克里斯 电子邮箱:chris@chris.com 有空位:没有

但我希望 Daniel 有空位说“不”

【问题讨论】:

  • 错字? Car::Car(unsigned int freeSeas) : freeSeats{ freeSeats } {} 构造函数参数freeSeas 从未在构造函数中使用。
  • warning: field 'freeSeats' is uninitialized when used here [-Wuninitialized], warning: unused parameter 'freeSeas' [-Wunused-parameter]。打开你的编译器警告!
  • 题外话:请不要写if(condition) return true; else return false;之类的代码——只要有return condition;,这样的代码更简洁。
  • @MarkStorer 在构造函数中有一个参数和一个同名的成员是完全可以的。当使用成员时,初始化列表的语法很明显(: member(...),在任何其他位置,它被参数隐藏(但可以通过例如this-&gt;member 引用)。
  • 我不同意必须更改名称以使其不同。通常成员字段和构造函数参数将具有相同的名称,因为它们是相同的并且它们的含义相同。强制规则为这两者使用不同的名称不可避免地会导致匈牙利表示法(m_field {field}field {providedField} 之类的东西)。

标签: c++ class boolean


【解决方案1】:

这里有一个错字:

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

您写的是freeSeas 而不是freeSeats。因此,freeSeas 参数未使用,freeSeats{ freeSeats } 什么也不做,因为freeSeats 引用的是成员变量,而不是参数。

【讨论】:

    【解决方案2】:

    启用编译器警告后,调试会更容易。编译器是你的朋友,如果你愿意的话,它会给你很大的帮助。

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

    它让我仔细观察了这个构造函数,然后你就可以看到错字了。

    【讨论】:

      猜你喜欢
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多