【发布时间】:2020-07-31 20:43:13
【问题描述】:
#include<iostream.h>
#include<conio.h>
class Node
{
public:
int data;
Node *next;
Node(int data)
{
data = data;
}
};
class LinkedList
{
Node *head;
Node *tail;
int n,data;
Node nod;
public:
void cll()
{
cout<<"Enter the no. of nodes"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>data;
nod = new Node(data);
if(head == NULL)
{
head = nod;
tail = nod;
}
else
{
tail->next = nod;
tail = nod;
}
}
}
};
void main()
{
LinkedList l1;
l1.cll();
}
当我编译这段代码时,我得到一个错误,说编译器无法为类生成默认构造函数。
如果我定义了一个构造函数,那么它会显示这个错误找不到默认构造函数来初始化基类 c++。
如何解决这个错误请帮忙。
【问题讨论】:
-
您的
Node类没有默认构造函数,但您在派生类中使用它(未初始化)。还有你认为data = data;这行会做什么?我知道它没有做什么。 -
错误信息准确地说明了问题所在。如果
Node没有默认构造函数,则LinkedList无法初始化其nod成员变量。 -
错误不止于此。从这个千年开始,你应该将你的编译器升级到一个。
-
void main()在 C++ 中是非法的。
标签: c++ compiler-errors