C++ 错误:没有合适的默认构造函数可用

C++ 错误:没有合适的默认构造函数可用,c++,list,class,object,compiler-errors,C++,List,Class,Object,Compiler Errors,我用node和迭代器实现了一个list类,它创建了一个类型为Ticket的列表,Ticket是我在类中定义的对象,但是当我试图编译它时,它说list没有默认构造函数,而显然有。有人看到这个问题了吗 这是类定义 class List : public Ticket { public: List(); void tick_print(vector<Ticket*> halfticks, int i); void push_back(Ticket* data);

我用node和迭代器实现了一个list类,它创建了一个类型为Ticket的列表,Ticket是我在类中定义的对象,但是当我试图编译它时,它说list没有默认构造函数,而显然有。有人看到这个问题了吗

这是类定义

class List : public Ticket
{
public:

    List();

    void tick_print(vector<Ticket*> halfticks, int i);
    void push_back(Ticket* data);
    void insert(Iterator iter, Ticket* s);

    Iterator erase(Iterator iter);

    Iterator begin();

    Iterator end();
private:
    Node* first;
    Node* last;
    friend class Iterator;
};
这就是我创建列表的地方

List ticks;
这是车票的代码

#include <iostream>
#include <string>
using namespace std;

class Season;
class Monthly;
class Daily;
class Half;

class Ticket
{
public:

    Ticket(int m, int d, int y, int h);
    virtual void display();
    virtual bool isvalid(int cm, int cd, int cy, int ch);
    virtual int getyear();
    virtual int getmonth();
    virtual int getday();
    virtual int gethour();

protected:

    int month;
    int day;
    int year;
    int hour;
    int hour2;
};

class Season: public Ticket
{
public:

    Season(string t, int m, int d, int y, int h);
    void display();
    bool isvalid(int cm, int cd, int cy, int ch);

protected:

    string type;
};

class Monthly: public Ticket
{
public:

    Monthly(string t, int m, int d, int y, int h);
    void display();
    bool isvalid(int cm, int cd, int cy, int ch);

protected:

    string type;
};

class Daily: public Ticket
{
public:

    Daily(string t, int m, int d, int y, int h);
    void display();
    bool isvalid(int cm, int cd, int cy, int ch);

protected:

    string type;
};

class Half: public Ticket
{
public:

    Half(string t, int m, int d, int y, int h);
    void display();
    bool isvalid(int cm, int cd, int cy, int ch);
    int getyear();
    int getmonth();
    int getday();
    int gethour();

protected:

    string type;
};

Ticket::Ticket(int m, int d, int y, int h)
{
    month = m;
    day = d;
    year = y;
    hour = h;
    hour2 = h+4;
}
void Ticket::display()
{
}
bool Ticket::isvalid(int cm, int cd, int cy, int ch)
{
    return 0;
}
int Ticket::getyear()
{
    return year;
}
int Ticket::getmonth()
{
    return month;
}
int Ticket::getday()
{
    return day;
}
int Ticket::gethour()
{
    return hour;
}

Season::Season(string t, int m, int d, int y, int h)
    :Ticket(m, d, y, h)
{
    type = t;
}
void Season::display()
{
    cout << type + " Ticket - ";
    cout << year;
}
bool Season::isvalid(int cm, int cd, int cy, int ch)
{
    if(year == cy)
        return true;
    else
        return false;
}

Monthly::Monthly(string t, int m, int d, int y, int h)
    :Ticket(m, d, y, h)
{
    type = t;
}
void Monthly::display()
{
    cout << type + " Ticket - ";
    cout << month << "/" << year;
}
bool Monthly::isvalid(int cm, int cd, int cy, int ch)
{
    if(year == cy && month == cm)
        return true;
    else
        return false;
}


Daily::Daily(string t, int m, int d, int y, int h)
    :Ticket(m, d, y, h)
{
    type = t;
}
void Daily::display()
{
    cout << type + " Ticket - ";
    cout << month << "/" << day << "/" << year;
}
bool Daily::isvalid(int cm, int cd, int cy, int ch)
{
    if(year == cy && month == cm && day == cd)
        return true;
    else
        return false;
}


Half::Half(string t, int m, int d, int y, int h)
    :Ticket(m, d, y, h)
{
    type = t;
}
void Half::display()
{
    cout << type + " Ticket - ";
    cout << month << "/" << day << "/" << year << " from " << hour << " to " << hour2;
}
bool Half::isvalid(int cm, int cd, int cy, int ch)
{
    if(year == cy && month == cm && day == cd)
    {
        if(ch >= 9 && ch <= 13)
        {
            if(ch >= hour && ch <= hour2)
                return true;
            else 
                return false;
        }
        else
            return false;
    }
    else
        return false;
}
int Half::getyear()
{
    return year;
}
int Half::getmonth()
{
    return month;
}
int Half::getday()
{
    return day;
}
int Half::gethour()
{
    return hour;
}
#包括
#包括
使用名称空间std;
上课季节;
每月上课;
每天上课;
半班;
班票
{
公众:
票证(整数m、整数d、整数y、整数h);
虚拟虚空显示();
虚拟布尔值有效(int-cm、int-cd、int-cy、int-ch);
虚拟整型getyear();
虚拟整数getmonth();
虚拟int getday();
虚拟整数gethour();
受保护的:
整月;
国际日;
国际年;
整小时;
内部时间2;
};
上课季节:公众票
{
公众:
季节(字符串t、整数m、整数d、整数y、整数h);
void display();
bool是有效的(int-cm,int-cd,int-cy,int-ch);
受保护的:
字符串类型;
};
月票班:公众票
{
公众:
每月(字符串t、整数m、整数d、整数y、整数h);
void display();
bool是有效的(int-cm,int-cd,int-cy,int-ch);
受保护的:
字符串类型;
};
每日班次:公众票
{
公众:
每日(字符串t、整数m、整数d、整数y、整数h);
void display();
bool是有效的(int-cm,int-cd,int-cy,int-ch);
受保护的:
字符串类型;
};
半舱:公众票
{
公众:
半字符串(字符串t,int m,int d,int y,int h);
void display();
bool是有效的(int-cm,int-cd,int-cy,int-ch);
int getyear();
int getmonth();
int getday();
int gethour();
受保护的:
字符串类型;
};
票证:票证(整数m,整数d,整数y,整数h)
{
月=m;
d=d;
年份=y;
小时=小时;
小时r2=h+4;
}
作废票证::显示()
{
}
bool票证::isvalid(整数厘米,整数cd,整数cy,整数ch)
{
返回0;
}
int票证::getyear()
{
回归年;
}
int Ticket::getmonth()
{
返回月份;
}
intticket::getday()
{
回归日;
}
intticket::gethour()
{
返程时间;
}
季节::季节(字符串t、整数m、整数d、整数y、整数h)
:车票(m、d、y、h)
{
类型=t;
}
无效季节::显示()
{

cout
Ticket
没有默认构造函数,因此不能默认构造
List
,因为它继承自
Ticket
,并且
List
Ticket

Ticket
中不调用基构造函数,因此
List
没有默认构造函数,因此不能被删除t-constructed,因为它继承自
Ticket
,并且
List
不调用
Ticket

中的基类构造函数。如果在派生类构造函数的初始化列表中不包含初始化基类的代码,则基类将使用默认构造函数初始化。换句话说

List::List()
{
}
相当于:

List::List() : Ticket()
{
}
由于
Ticket
没有默认构造函数,编译器无法从发布的代码初始化
Ticket

您可以通过以下方法之一解决此问题:

  • 将默认构造函数添加到
    Ticket
  • 更新
    List::List()
    以使用初始化列表中唯一的
    Ticket
    构造函数

    List::List() : Ticket(0, 0, 0) // You need to figure out what values
                                   // make sense in your application.
    {
    }
    

  • 如果在派生类的构造函数的初始化列表中不包含初始化基类的代码,则使用默认构造函数初始化基类。换句话说

    List::List()
    {
    }
    
    相当于:

    List::List() : Ticket()
    {
    }
    
    由于
    Ticket
    没有默认构造函数,编译器无法从发布的代码初始化
    Ticket

    您可以通过以下方法之一解决此问题:

  • 将默认构造函数添加到
    Ticket
  • 更新
    List::List()
    以使用初始化列表中唯一的
    Ticket
    构造函数

    List::List() : Ticket(0, 0, 0) // You need to figure out what values
                                   // make sense in your application.
    {
    }
    

  • 票证的代码在哪里
    ?这很可能就是问题所在。我会将其添加到保留中on@beginnerjohn:在不删除不相关代码的情况下,不要添加代码!
    列表
    不应从
    票证
    继承。列表包含票证。列表本身不是票证。
    票证
    的代码在哪里?这是最重要的凯莉:问题出在哪里。我会把它加在保留中on@beginnerjohn:在不删除无关代码的情况下,请勿添加代码!
    List
    不应从
    Ticket
    继承。列表包含Ticket。列表本身不是Ticket。定义派生类时,不要求基类具有默认构造函数!但是,当基类中没有默认构造函数您需要在成员初始值设定项列表中提供构造函数参数。定义派生类时,不要求基类具有默认构造函数!但是,如果基类中没有默认构造函数,则需要在成员初始值设定项列表中提供构造函数参数圣。