gpt4 book ai didi

c++ - 编译器无法为类Error生成默认构造函数

转载 作者:行者123 更新时间:2023-12-02 10:56:21 24 4
gpt4 key购买 nike

#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++。
我该如何解决此错误,请帮助。

最佳答案

编译代码:

#include<iostream>//change here
//#include<conio.h> //change here

class Node
{
public:
int data;
Node *next;
Node(int data)
{
data = data;
}
};

class LinkedList
{
Node *head;
Node *tail;
int n,data;
Node *nod; //change here
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;
}
}
}
};

int main() //change here
{
LinkedList l1;
l1.cll();
}

关于c++ - 编译器无法为类Error生成默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63187233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com