本文介绍了错误没有合适的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用节点和迭代器实现一个列表类,该列表类创建一个类型为Ticket的列表,这是我在一个类中定义的对象,但是当我尝试编译它时,它说List没有默认的构造函数当有明显的时候.有人在这里看到这个问题吗?
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?
这是类定义
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;
}
这是我创建列表的地方
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
没有默认构造函数,因此List
不能默认构造,因为它继承自Ticket
和不会在Ticket
中调用基本构造函数.
Ticket
doesn't have a default constructor, therefore List
can't be default-constructed since it inherits from Ticket
, and List
does not call a base constructor in Ticket
.
这篇关于错误没有合适的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!