简体   繁体   中英

Error no appropriate default constructor available

I am implementing a list class with node and iterator, that creates a list of type Ticket, which is an object that i have defined in a class, but when i try to compile it says that there is no default constructor for List when there clearly is. Does anyone see the issue here?

This is the class definition

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

This is the implementation of the default constructor

List::List()
{
    first = NULL;
    last = NULL;
}

And this is where i create the list

List ticks;

Here is the code for ticket

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

Ticket没有默认的构造函数,因此List不能默认构造,因为它继承了Ticket ,并且List不会在Ticket调用基本构造函数。

When you don't include code to initialize a base class in the initialization list of a constructor of a derived class, the base class is initialized using the default constructor. In other words,

List::List()
{
}

is equivalent to:

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

Since Ticket does not have a default constructor, the compiler does not have any way to initialize Ticket from the posted code.

You can resolve this problem by one of the following methods:

  1. Add a default constructor to Ticket
  2. Update List::List() to use the only constructor of Ticket in the initialization list.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM