C++ 错误:隐式声明的定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47092536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:41:54  来源:igfitidea点击:

C++ error: definition of implicitly-declared

c++compiler-errors

提问by Leslie Zhou

I'm writing this linked list program with C++

我正在用 C++ 编写这个链表程序

When I test the program, I got the error

当我测试程序时,出现错误

linkedlist.cpp:5:24: error: definition of implicitly-declared 'constexpr LinkedList::LinkedList()' LinkedList::LinkedList(){

Linkedlist.cpp:5:24: 错误:隐式声明的定义'constexpr LinkedList::LinkedList()' LinkedList::LinkedList(){

Here's the code

这是代码

linkedlist.h file:

链表.h文件:

#include "node.h"
using namespace std;

class LinkedList {
  Node * head = nullptr;
  int length = 0;
public:
  void add( int );
  bool remove( int );
  int find( int );
  int count( int );
  int at( int );
  int len();
};

linkedlist.cpp file:

链表.cpp文件:

#include "linkedlist.h"
#include <iostream>
using namespace std;

LinkedList::LinkedList(){
  length = 0;
  head = NULL;
}
/*and all the methods below*/

please help.

请帮忙。

回答by CinCout

Declare the parameterless constructor in the header file:

在头文件中声明无参数构造函数:

class LinkedList {
{
....
public:
    LinkedList();
    ....
}

You are defining it in the .cpp file without actually declaring it. But since the compiler provides such a constructor by default (if no other constructor is declared), the error clearly states that you are trying to define an implicitly-declared constructor.

您是在 .cpp 文件中定义它而不实际声明它。但是由于编译器默认提供了这样的构造函数(如果没有声明其他构造函数),错误清楚地表明您正在尝试定义一个隐式声明的构造函数。