为什么我的代码说"Yes"什么时候应该说"No"?
Why does my code say "Yes" when it should say "No"?
当将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 }
^~~~~~~~~
我不必理解所有内容,但它告诉我两件事:
- 有未使用的参数(为什么?它用于初始化...(
- 变量使用未初始化的值初始化(为什么?
它让我仔细观察这个构造函数,然后你可以看到拼写错误。
相关文章:
- "error: no matching function for call to"构造函数错误
- c++库的公共头文件中应该包含什么
- 调用专用模板时出错"no matching function for call to [...]"
- 我应该使用什么来代替void作为变体中的替代类型之一
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 为什么我的代码说"Yes"什么时候应该说"No"?
- 提升序列化仅适用于主要?当我在其他对象中使用时,继续说"has no member named ‘serialize’"
- 这个给定的代码应该将给定的数字转换为尽可能滞后的数字,no.by 用 9.It 替换合适的数字是行不通的
- 为什么"no temporary std::string objects would arise"在有效的现代C++的第25项中说?
- 为什么名为互斥锁文档的加速间进程说每个进程都应该有自己的名称互斥锁?
- 我应该做哪些更改,以便当我输入"a"时它说面包,当我输入时"b"它说葡萄酒?
- NetBeans for C/C++ 说"no compiler found"
- 从命令行构建x64 C++项目说:此操作应该只在UI线程上进行
- 标准说实现在发现未知属性时应该做什么
- 为什么C/C++编码标准说我们不应该在函数中有多个返回
- 我应该声明一个方法 no除非它在正确使用时永远不会抛出
- Cppcheck 说 char[256] 应该在构造函数的初始值设定项列表中初始化
- 如何解决错误说"No matching function call for XXX"?
- 使用 qFromBigEndian 从 Char 转换为 Int 说: "No matching function for call to"