节点和链表的问题
Trouble with Nodes and Linked Lists
我有一个任务,我应该在其中创建在双向链表中插入和删除节点的方法。但是我对我的 C++ 有点生疏。
我的前指针和后指针出现错误。
LinkedList.h
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
using namespace std;
struct node {
node * prev;
int data;
node * next;
};
class LinkedList {
private:
//pointers to point to front and end of Linked List
static node * front; //the error is coming from here
static node * rear; //the error is coming from here
public:
static void insert_front(int data);
};
#endif
LinkedList.cpp
#include "LinkedList.h"
//insert int to front
void LinkedList::insert_front(int data) {
node *q = nullptr;
//If the list is empty
if (front == nullptr && rear == nullptr) {
q = new node;
q->prev = nullptr;
q->data = data;
q->next = nullptr;
front = q;
rear = q;
q = nullptr;
}
//If there is only one node in list
//...
//If there are at least 2 nodes in list
//...
}
我得到的错误是:
unresolved external symbol "private: static struct node * LinkedList::front (?front@LinkedList@@0PAUnode@@A)
unresolved external symbol "private: static struct node * LinkedList::rear (?rear@LinkedList@@0PAUnode@@A)
如果我在 cpp 文件中引用它们时从私有变量中删除 static,我会得到 "a nonstatic member reference must be relative to a specific object"
您必须在您的 cpp 文件中初始化静态变量:
node* LinkedList::front = nullptr;
node* LinkedList::rear = nullptr;
我们只能在 class 上调用静态 class 成员,而不能在 class 的对象上调用。这是可能的,即使不存在实例。这就是为什么每个静态成员实例 必须 初始化,通常在 cpp 文件中。
而且由于静态变量是在class范围之外初始化的,我们必须通过全名调用变量(例如LinkedList::front)。
您已成为 front
和 rear
成员 static
。这意味着对于 LinkedList
class.
的所有实例,这些成员只有一个实例
如果那是你想要的,那么你需要在 .cpp 文件中声明它们,正如@Soeren 所建议的:
node* LinkedList::front = nullptr;
node* LinkedList::read = nullptr;
但是,您可能想要的是能够创建多个 LinkedList
,并跟踪每个 front
和 rear
。如果是这种情况,那么您应该使这些成员不是静态的(并且还要使 insert_front()
non-static)。
执行此操作时出错的原因是因为您需要创建 class 的实例才能使用它:
LinkedList list;
list.insert_front(5);
我有一个任务,我应该在其中创建在双向链表中插入和删除节点的方法。但是我对我的 C++ 有点生疏。 我的前指针和后指针出现错误。
LinkedList.h
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
using namespace std;
struct node {
node * prev;
int data;
node * next;
};
class LinkedList {
private:
//pointers to point to front and end of Linked List
static node * front; //the error is coming from here
static node * rear; //the error is coming from here
public:
static void insert_front(int data);
};
#endif
LinkedList.cpp
#include "LinkedList.h"
//insert int to front
void LinkedList::insert_front(int data) {
node *q = nullptr;
//If the list is empty
if (front == nullptr && rear == nullptr) {
q = new node;
q->prev = nullptr;
q->data = data;
q->next = nullptr;
front = q;
rear = q;
q = nullptr;
}
//If there is only one node in list
//...
//If there are at least 2 nodes in list
//...
}
我得到的错误是:
unresolved external symbol "private: static struct node * LinkedList::front (?front@LinkedList@@0PAUnode@@A)
unresolved external symbol "private: static struct node * LinkedList::rear (?rear@LinkedList@@0PAUnode@@A)
如果我在 cpp 文件中引用它们时从私有变量中删除 static,我会得到 "a nonstatic member reference must be relative to a specific object"
您必须在您的 cpp 文件中初始化静态变量:
node* LinkedList::front = nullptr;
node* LinkedList::rear = nullptr;
我们只能在 class 上调用静态 class 成员,而不能在 class 的对象上调用。这是可能的,即使不存在实例。这就是为什么每个静态成员实例 必须 初始化,通常在 cpp 文件中。
而且由于静态变量是在class范围之外初始化的,我们必须通过全名调用变量(例如LinkedList::front)。
您已成为 front
和 rear
成员 static
。这意味着对于 LinkedList
class.
如果那是你想要的,那么你需要在 .cpp 文件中声明它们,正如@Soeren 所建议的:
node* LinkedList::front = nullptr;
node* LinkedList::read = nullptr;
但是,您可能想要的是能够创建多个 LinkedList
,并跟踪每个 front
和 rear
。如果是这种情况,那么您应该使这些成员不是静态的(并且还要使 insert_front()
non-static)。
执行此操作时出错的原因是因为您需要创建 class 的实例才能使用它:
LinkedList list;
list.insert_front(5);