C++ C++;如果条件不起作用

C++ C++;如果条件不起作用,c++,C++,我有一个对象数组 Passenger travellers[] = { Passenger(nullptr, "Toronto", 2018, 4, 20), Passenger("", "Toronto", 2018, 4, 20), Passenger("John Smith", nullptr, 2018, 4, 20), Passenger("John Smith", "", 2018, 4, 20), Passenger("John Smith"

我有一个对象数组

Passenger travellers[] = {
    Passenger(nullptr, "Toronto", 2018, 4, 20),
    Passenger("", "Toronto", 2018, 4, 20),
    Passenger("John Smith", nullptr, 2018, 4, 20),
    Passenger("John Smith", "", 2018, 4, 20),
    Passenger("John Smith", "Toronto", 2018, 4, 20), // valid
    Passenger("John Smith", "Toronto", 2028, 4, 20),
    Passenger("John Smith", "Toronto", 2014, 4, 20),
    Passenger("John Smith", "Toronto", 2020, 12, 31), // valid
    Passenger("John Smith", "Toronto", 2018, 40, 20),
    Passenger("John Smith", "Toronto", 2018, 0, 20),
    Passenger("John Smith", "Toronto", 2017, 1, 1), // valid
    Passenger("John Smith", "Toronto", 2018, 4, 0),
    Passenger("John Smith", "Toronto", 2018, 4, 32),
    Passenger(nullptr, nullptr, 0, 0, 0),
    Passenger()
};
我的构造函数是:

默认构造函数

Passenger::Passenger() {
    p_name[0] = '\0';
    p_dest[0] = '\0';

    // destination date
    d_yy = 0;
    d_mm = 0;
    d_dd = 0;
}
我的另一个带参数的构造函数是:

Passenger::Passenger(const char *name, const char *destination, int year, int month, int days) {

    if (name != nullptr && destination != nullptr && name[0] != '\0' && destination[0] != '\0') {
        if (year >= 2017 && year <= 2020 && month >= 1 && month <= 12 && days >= 1 && days <= 31) {
            strncpy(p_name, name, 32);
            strncpy(p_dest, destination, 32);
            d_yy = year;
            d_mm = month;
            d_dd = days;
        }
        else 
            Passenger();
    }
    else
        Passenger();
}
Passenger::Passenger(常量字符*名称,常量字符*目的地,整数年,整数月,整数天){
if(name!=nullptr&&destination!=nullptr&&name[0]!='\0'&&destination[0]!='\0'){
如果(年>=2017年&&year=1&&month=1&&days如下:

else {
    Passenger();
}
只需创建一个临时(立即销毁),您可能希望改为抛出,或者对于默认初始化,它将是:

Passenger::Passenger(const char *name,
                     const char *destination,
                     int year,
                     int month,
                     int days) : Passenger() // Delegating constructor
{
    if (name != nullptr
       && destination != nullptr
       && name[0] != '\0'
       && destination[0] != '\0') {
        if (year >= 2017 && year <= 2020
           && month >= 1 && month <= 12
           && days >= 1 && days <= 31) {
            strncpy(p_name, name, 32);
            strncpy(p_dest, destination, 32);
            d_yy = year;
            d_mm = month;
            d_dd = days;
        }
    }
}
Passenger::Passenger(const char*name,
常量字符*目的地,
整年,
整月,
int days):乘客()//委托构造函数
{
if(name!=nullptr
&&目的地!=nullptr
&&名称[0]!='\0'
&&目的地[0]!='\0'){
如果(年>=2017年&&year=1&&month=1&&days如下:

else {
    Passenger();
}
只需创建一个临时(立即销毁),您可能希望改为抛出,或者对于默认初始化,它将是:

Passenger::Passenger(const char *name,
                     const char *destination,
                     int year,
                     int month,
                     int days) : Passenger() // Delegating constructor
{
    if (name != nullptr
       && destination != nullptr
       && name[0] != '\0'
       && destination[0] != '\0') {
        if (year >= 2017 && year <= 2020
           && month >= 1 && month <= 12
           && days >= 1 && days <= 31) {
            strncpy(p_name, name, 32);
            strncpy(p_dest, destination, 32);
            d_yy = year;
            d_mm = month;
            d_dd = days;
        }
    }
}
Passenger::Passenger(const char*name,
常量字符*目的地,
整年,
整月,
int days):乘客()//委托构造函数
{
if(name!=nullptr
&&目的地!=nullptr
&&名称[0]!='\0'
&&目的地[0]!='\0'){

如果(年份>=2017&&year=1&&month=1&&days您无法从另一个
乘客(…)
构造函数的体内调用默认的
乘客()
构造函数

实际上,您正在构建立即超出范围的临时
Passenger
对象:

Passenger::Passenger(const char *name, const char *destination, int year, int month, int days)
{
    if (name != nullptr && destination != nullptr && name[0] != '\0' && destination[0] != '\0')
    {
        if (year >= 2017 && year <= 2020 && month >= 1 && month <= 12 && days >= 1 && days <= 31)
        {
            strncpy(p_name, name, 32);
            strncpy(p_dest, destination, 32);
            d_yy = year;
            d_mm = month;
            d_dd = days;
        }
        else {
            Passenger(); // <-- TEMP OBJECT!!
        }
    }
    else {
        Passenger(); // <-- TEMP OBJECT!!
    }
}
Passenger::Passenger(常量字符*名称,常量字符*目的地,整数年,整数月,整数天)
{
if(name!=nullptr&&destination!=nullptr&&name[0]!='\0'&&destination[0]!='\0')
{

如果(年份>=2017&&year=1&&month=1&&days您无法从另一个
乘客(…)
构造函数的体内调用默认的
乘客()
构造函数

实际上,您正在构建立即超出范围的临时
Passenger
对象:

Passenger::Passenger(const char *name, const char *destination, int year, int month, int days)
{
    if (name != nullptr && destination != nullptr && name[0] != '\0' && destination[0] != '\0')
    {
        if (year >= 2017 && year <= 2020 && month >= 1 && month <= 12 && days >= 1 && days <= 31)
        {
            strncpy(p_name, name, 32);
            strncpy(p_dest, destination, 32);
            d_yy = year;
            d_mm = month;
            d_dd = days;
        }
        else {
            Passenger(); // <-- TEMP OBJECT!!
        }
    }
    else {
        Passenger(); // <-- TEMP OBJECT!!
    }
}
Passenger::Passenger(常量字符*名称,常量字符*目的地,整数年,整数月,整数天)
{
if(name!=nullptr&&destination!=nullptr&&name[0]!='\0'&&destination[0]!='\0')
{

如果(年份>=2017&&year=1&&month=1&&days)您能否包含
乘客的完整定义(特别是每个数据成员的类型)信息不足。您没有指定
乘客的成员类型。
乘客()第二个构造函数中的
语句不会“重置”对象-它们会创建一个立即停止存在的临时对象。如果没有对
乘客
类的定义,除此之外,没有人能为您提供帮助。您能否包括
乘客
的完整定义(特别是每个数据成员的类型)信息不足。您尚未指定
Passenger
的成员类型。第二个构造函数中的
Passenger()
语句未“重置”对象-它们创建了一个立即不存在的临时对象。如果没有对
乘客
类的定义,除此之外,没有人能为您提供帮助。