【问题标题】:Error no appropriate default constructor available错误没有合适的默认构造函数可用
【发布时间】:2016-01-22 11:42:26
【问题描述】:

我正在实现一个带有节点和迭代器的列表类,它创建了一个 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;
}

【问题讨论】:

  • Ticket 的代码在哪里?这很可能是问题所在。
  • 我会在等待中添加它
  • @beginnerjohn:在不删除不相关代码的情况下不要添加代码!
  • List 不应继承自 Ticket。一个列表包含票。列表本身并不是一张票。

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


【解决方案1】:

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

List::List()
{
}

相当于:

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

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 在定义派生类时,基类不需要有默认构造函数!但是,当基类中没有默认构造函数时,您需要在成员初始值设定项列表中提供构造函数参数。
    猜你喜欢
    • 2016-03-28
    • 2013-08-25
    • 2012-10-10
    • 2014-12-31
    • 2012-12-16
    • 2014-01-31
    • 2014-05-26
    • 2023-03-26
    相关资源
    最近更新 更多