Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ error: definition of implicitly-declared

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

When I test the program, I got the error

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

Here's the code

linkedlist.h file:

#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:

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

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

please help.

like image 538
Leslie Zhou Avatar asked Nov 03 '17 09:11

Leslie Zhou


1 Answers

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.

like image 91
CinCout Avatar answered Oct 11 '22 18:10

CinCout