【发布时间】:2017-06-15 00:46:19
【问题描述】:
我有一个任务,我应该在其中创建在双向链表中插入和删除节点的方法。但是我对我的 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 文件中引用私有变量时从私有变量中删除它们,我会得到“非静态成员引用必须相对于特定对象”
【问题讨论】:
标签: c++ pointers static linked-list private