错误没有适当的默认构造函数

时间:2015-10-23 02:18:30

标签: c++ list class object compiler-errors

我正在实现一个带有node和iterator的列表类,它创建了一个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::List()
{
    first = NULL;
    last = NULL;
}

这就是我创建列表的地方

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

2 个答案:

答案 0 :(得分:1)

Ticket没有默认构造函数,因此List不能默认构造,因为它继承自Ticket,而List不会调用基础Ticket中的构造函数。

答案 1 :(得分:1)

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

List::List()
{
}

相当于:

List::List() : Ticket()
{
}

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

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

  1. Ticket
  2. 添加默认构造函数
  3. 更新List::List()以使用初始化列表中唯一的Ticket构造函数。

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