C 错误:隐式声明的定义

新手上路,请多包涵

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

当我测试程序时,我得到了错误

linkedlist.cpp:5:24:错误:隐式声明的“constexpr LinkedList::LinkedList()”的定义 LinkedList::LinkedList(){

这是代码

链接列表.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();
};

链接列表.cpp 文件:

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

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

请帮忙。

原文由 Leslie Zhou 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 417
1 个回答

您应该在类内声明构造函数,以便在类外定义 cunstructor。否则你应该在类本身中定义它。你的类应该是这样的。

             #include "node.h"
            using namespace std;
            class LinkedList {
              Node * head = nullptr;
              int length = 0;
            public:
              LinkedList();
              void add( int );
              bool remove( int );
              int find( int );
              int count( int );
              int at( int );
              int len();
            };

原文由 JOSEPH BENOY 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题