我无法在列表中显示旅行者。它显示日期,但不显示名称和目的地。我不知道如何解决这个问题。
这是下面的代码。
Travler.header
#ifndef SICT_TRAVELER_H
#define SICT_TRAVELER_H
namespace sict {
class Traveler {
private:
char max_destination_size[32];
char max_name_size[16];
char max_name_sizelast[16];
int yearofdepart;
int monthofdepart;
int dayofdepart;
public:
Traveler();
Traveler(const char *, const char *, const char *);
Traveler(const char *, const char *, const char *, int, int, int);
bool isEmpty()const;
void display()const;
const char* name() const;
bool canTravelWith(const Traveler&) const;
};
}
#endif
Taveler.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Traveler.h"
#include <cstring>
#include <string.h>
using namespace std;
namespace sict {
Traveler::Traveler() {
max_destination_size[0] = '\0';
max_name_size[0] = '\0';
max_name_sizelast[0] = '\0';
yearofdepart = 0;
monthofdepart = 0;
dayofdepart = 0;
}
Traveler::Traveler(const char * travlerfirstname, const char * travelerlastname, const char * destination) {
bool nameinvalid = travlerfirstname && travelerlastname == nullptr;
bool destinvalid = destination == nullptr;
bool invalid = nameinvalid && destinvalid;
if (!invalid) {
yearofdepart = 2019;
monthofdepart = 7;
dayofdepart = 1;
}
}
Traveler::Traveler(const char * travelerfirstname, const char * travelerlastname, const char * destination, int xyear, int xmonth, int xday)
{
bool yearvaild = xyear >= 2019 && xyear <= 2022;
bool monthvaild = xmonth >= 1 && xmonth <= 12;
bool dayvaild = xday >= 1 && xday <= 31;
if (travelerfirstname == nullptr || travelerlastname == nullptr || destination == nullptr || !yearvaild || !monthvaild || !dayvaild) {
*this = Traveler();
}
else {
strncpy(max_name_size, travelerfirstname, 15);
strncpy(max_name_sizelast, travelerlastname, 15);
strncpy(max_destination_size, destination, 31);
yearofdepart = xyear;
monthofdepart = xmonth;
dayofdepart = xday;
}
}
bool Traveler::isEmpty()const {
bool valid = strlen(max_name_size) > 0 && strlen(max_name_sizelast) > 0 && strlen(max_destination_size) > 0 && dayofdepart != 0 && monthofdepart != 0 && yearofdepart != 0;
return !valid;
}
void Traveler::display()const {
if (max_name_size[0] == '\0' || max_name_sizelast[0] == '\0' || max_destination_size == '\0' || yearofdepart == 0 || monthofdepart == 0 || dayofdepart == 0) {
cout << "--> Not a valid traveler! <--" << endl;
}
else {
if (monthofdepart > 0 && monthofdepart >= 6 && dayofdepart <= 7) {
cout << max_name_sizelast << ", " << max_name_size << " goes to " << max_destination_size << " on " << yearofdepart << "/0" << monthofdepart << "/0" << dayofdepart << endl;
} else if(monthofdepart > 0 && monthofdepart == 6){
cout << max_name_sizelast << ", " << max_name_size << " goes to " << max_destination_size << " on " << yearofdepart << "/0" << monthofdepart << "/" << dayofdepart << endl;
}
else {
cout << max_name_sizelast << ", " << max_name_size << " goes to " << max_destination_size << " on " << yearofdepart << "/" << monthofdepart << "/" << dayofdepart << endl;
}
}
}
const char * Traveler::name() const {
return max_name_size, max_name_sizelast;
}
bool Traveler::canTravelWith(const Traveler& p) const {
bool xday = (*this).dayofdepart == p.dayofdepart;
bool xmonth = (*this).monthofdepart == p.monthofdepart;
bool xyear = (*this).yearofdepart == p.yearofdepart;
bool match = xday && xmonth && xyear;
return match;
}
}
这是主要的源文件。
#include <iostream>
#include "Traveler.h"
using namespace std;
using namespace sict;
int main()
{
Traveler travelers[] = {
Traveler(nullptr, "Smith", "Toronto", 2020, 4, 20),
Traveler("", "Smith", "Toronto", 2020, 4, 20),
Traveler("John", "", "Toronto", 2020, 4, 20),
Traveler("John", "Smith", nullptr, 2020, 4, 20),
Traveler("John", "Smith", "", 2020, 4, 20),
Traveler("John", "Smith", "Toronto", 2020, 4, 20), // valid
Traveler("John", "Smith", "Toronto", 2028, 4, 20),
Traveler("John", "Smith", "Toronto", 2014, 4, 20),
Traveler("John", "Smith", "Toronto", 2022, 12, 31), // valid
Traveler("John", "Smith", "Toronto", 2020, 40, 20),
Traveler("John", "Smith", "Toronto", 2020, 0, 20),
Traveler("John", "Smith", "Toronto", 2019, 1, 1), // valid
Traveler("John", "Smith", "Toronto", 2020, 4, 0),
Traveler("John", "Smith", "Toronto", 2020, 4, 32),
Traveler(nullptr, nullptr, nullptr, 0, 0, 0),
Traveler("John", "Smith", "Toronto"), // valid
Traveler()
};
cout << "----------------------------------------" << endl;
cout << "Testing the validation logic" << endl;
cout << "(only travelers 6, 9, 12 and 16 should be valid)" << endl;
cout << "----------------------------------------" << endl;
for (int i = 0; i < 17; ++i)
{
cout << "Traveler " << i + 1 << ": "
<< (travelers[i].isEmpty() ? "not valid" : "valid") << endl;
}
cout << "----------------------------------------" << endl << endl;
Traveler david("David", "Davis", "Toronto", 2019, 6, 20);
Traveler friends[] = {
Traveler("Vanessa", "Miller", "Toronto", 2019, 6, 20),
Traveler("John", "Miller", "Toronto", 2019, 6, 6),
Traveler("Alice", "Turner", "Toronto", 2019, 10, 20),
Traveler("Bob", "Moore", "Paris", 2019, 6, 20),
Traveler("Jennifer", "Hill", "Toronto", 2020, 6, 20),
Traveler("Mike", "Flores", "Toronto", 2019, 6, 20),
Traveler("Sarah", "Stewart", "Toronto", 2019, 6, 20),
Traveler("Mark", "Simmons", "Toronto") <-- This part I need to solve
};
cout << "----------------------------------------" << endl;
cout << "Testing Traveler::display(...)" << endl;
cout << "----------------------------------------" << endl;
for (int i = 0; i < 8; ++i)
friends[i].display();
cout << "----------------------------------------" << endl << endl;
cout << "----------------------------------------" << endl;
cout << "Testing Traveler::canTravelWith(...)" << endl;
cout << "----------------------------------------" << endl;
cout << david.name() << " can travel with: " << endl;
for (int i = 0; i < 8; ++i)
{
if (david.canTravelWith(friends[i]))
cout << " - " << friends[i].name() << endl;
}
cout << "----------------------------------------" << endl << endl;
return 0;
}
这是我当前的输出。
----------------------------------------
Testing the validation logic
(only travelers 6, 9, 12 and 16 should be valid)
----------------------------------------
Traveler 1: not valid
Traveler 2: not valid
Traveler 3: not valid
Traveler 4: not valid
Traveler 5: not valid
Traveler 6: valid
Traveler 7: not valid
Traveler 8: not valid
Traveler 9: valid
Traveler 10: not valid
Traveler 11: not valid
Traveler 12: valid
Traveler 13: not valid
Traveler 14: not valid
Traveler 15: not valid
Traveler 16: valid
Traveler 17: not valid
----------------------------------------
----------------------------------------
Testing Traveler::display(...)
----------------------------------------
Miller, Vanessa goes to Toronto on 2019/06/20
Miller, John goes to Toronto on 2019/06/06
Turner, Alice goes to Toronto on 2019/10/20
Moore, Bob goes to Paris on 2019/06/20
Hill, Jennifer goes to Toronto on 2020/06/20
Flores, Mike goes to Toronto on 2019/06/20
Stewart, Sarah goes to Toronto on 2019/06/20
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠π, ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠π goes to ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠π on 2019/07/01 <-- the problem
(The last one should be like this "Simmons, Mark goes to Toronto on 2019/07/01")
----------------------------------------
----------------------------------------
Testing Traveler::canTravelWith(...)
----------------------------------------
Davis can travel with:
- Miller
- Moore
- Flores
- Stewart
----------------------------------------
那么任何人都可以指出我缺少什么或者我的代码有什么样的问题?
答案 0 :(得分:0)
问题在于构造函数定义:
Traveler::Traveler(const char * travlerfirstname, const char * travelerlastname, const char * destination) {
bool nameinvalid = travlerfirstname && travelerlastname == nullptr;
bool destinvalid = destination == nullptr;
bool invalid = nameinvalid && destinvalid;
if (!invalid) {
yearofdepart = 2019;
monthofdepart = 7;
dayofdepart = 1;
}
}
此处,字段max_name_size
,max_name_sizelast
和max_destination_size
未初始化。
以下调用将调用完全相同的构造函数:
Traveler("Mark", "Simmons", "Toronto") <-- This part I need to solve
相反,您应该这样做:
Traveler::Traveler(const char * travlerfirstname, const char * travelerlastname, const char * destination) {
bool nameinvalid = travlerfirstname && travelerlastname == nullptr;
bool destinvalid = destination == nullptr;
bool invalid = nameinvalid && destinvalid;
if (!invalid) {
strncpy(max_name_size, travlerfirstname, 15);
strncpy(max_name_sizelast, travelerlastname, 15);
strncpy(max_destination_size, destination, 31);
yearofdepart = 2019;
monthofdepart = 7;
dayofdepart = 1;
}
}
建议:
请了解有关委托构造函数的信息。这种类型的编码总是会在以后以错误和可维护性问题的形式回击您。