【问题标题】:Compiler could not generate default constructor for the class Error编译器无法为类错误生成默认构造函数
【发布时间】: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


【解决方案1】:

可编译代码:

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

【讨论】:

  • 请注意,好的答案不仅提供固定代码,还提供有关所有修复的说明。
【解决方案2】:

标准说:

如果没有为类类型(结构、类或联合)提供任何类型的用户声明构造函数,编译器将始终将默认构造函数声明为其类的内联公共成员。

由于您提供了一个采用 int 的构造函数,因此没有生成默认构造函数。

class Node
{
  public:
  int data;
  Node *next;
  Node(int data) { // here is your custom constructor
    data = data;
  }
};

然后你创建一个对象:

Node nod; // no constructor Node() provided - this is the error you get
Node nod(1); // will work, since you have provided Node(int data) constructor

但显然根据您的代码,您应该声明指向 Node 的指针,而不是创建对象。

Node *nod = new Node(1);

并且更好地重新定义您的构造函数以使用成员初始化器列表:

class Node
{
  public:
  int data;
  Node *next;

  Node(int data) : data{data} { }
};

【讨论】:

    【解决方案3】:

    此行导致错误

    Node nod;
    

    相反,你应该写

    Node* nod;
    

    无论如何,您的代码有很多错误导致程序崩溃。可能您正在学习过时的 C++ 课程。

    首先使用#include &lt;iostream&gt; 而不是#include&lt;iostream.h&gt;。这个程序实际上不需要标题&lt;conio.h&gt;。 另一方面,尝试学习智能指针而不是 C 风格的指针。因此,如果您想在同一个文件中执行所有操作,您的 Node 类应该如下所示。强烈建议将.h.cpp中的声明和实现分别分开

    #include <iostream>
    #include <memory>
    
    class Node
    {
    public:
        
        int data;
        std::shared_ptr<Node> nextNode;
        inline Node() : nextNode { nullptr }{};
        inline Node(int data) : data{ data }, nextNode{nullptr} {}; //member initializer list
    };
    

    在构造函数上使用成员初始化器列表也是一个好习惯。

    LinkedList 应该是这样的

    class LinkedList
    {
        std::shared_ptr<Node> head;
        std::shared_ptr<Node> tail;
        std::shared_ptr<Node> currentNode;
        int n, data;
    public:
        LinkedList() : head{ nullptr }, tail{ nullptr }, currentNode{ nullptr } {};
        void cll()
        {
            std::cout << "Enter the no. of nodes" << std::endl;
            std::cin >> n;
            for (int i = 0; i < n; i++)
            {
                std::cout << "Enter the data for the " << i + 1 << " node" << std::endl;
                std::cin >> data; 
                currentNode = std::make_shared<Node>(data);
                if (!head) // head == nullptr is redundant
                {
                    head = currentNode;
                    tail = currentNode;
                }
                else
                {
                    tail->nextNode = currentNode;
                    tail = currentNode;
                }
            }
        }
    };
    

    最后,void main() 在现代 C++ 中是不允许的,您应该使用 int main()

    int main()
    {
        LinkedList l1;
        l1.cll();
    }
    

    此代码编译正确,但我强烈建议您更改学习 C++ 的源代码

    还有一件事,使用nullptr insted of NULL

    【讨论】:

    • 感谢您的帮助。你能推荐一些学习 C++ 的好资源吗?
    • 不客气。除了大学之外,我用来学习 C++ 的一些资源是“Sams 每天一小时自学 C++”一书、YouTube 频道“The Cherno”和 Udemy 课程“Beginning C++ Programming - From Beginner to Beyond”。我希望我的解决方案对您有所帮助。祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2020-09-25
    • 1970-01-01
    • 2016-10-16
    • 2018-10-01
    相关资源
    最近更新 更多