【发布时间】:2017-04-28 15:31:59
【问题描述】:
我正在尝试构建一个循环链表,该链表按间隔排列并删除它所在的节点。但我不断收到未声明的标识符错误。我似乎无法修复此错误。
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node() {data = 0;next = NULL;}
Node(int x) {data = x;next = NULL;}
};
class CircularLinkedList {
public:
int addAtFront(Node *n);
int isEmpty();
int addAtEnd(Node *n);
CircularLinkedList() {head = NULL;}
Node *head;
Node* search(int k);
Node* deleteNode(int x);
};
int search(int x) {
Node *ptr = head;
while(ptr != NULL && ptr->data != x) {
ptr = ptr->next;
}
return ptr;
}
int addAtFront(Node *n) {
int i = 0;
if(head == NULL) { //error: undeclared identifier 'head'
n->next = head;
head = n; //error: undeclared identifier 'head'
i++;
}
else {
n->next = head; //error: undeclared identifier 'head'
Node* last = getLastNode(); //error: undeclared identifier 'getLastNode'
last->next = n;
head = n; //error: undeclared identifier 'head'
i++;
}
return i;
}
int deleteNode(int x) {
Node *n = search(x); //error: Cannot initialize a variable of type 'Node *' with an rvalue of type 'int'
Node *ptr = head; //error: undeclared identifier 'head'
if(ptr == NULL) {
cout << "List is empty";
return NULL;
}
else if(ptr == n) {
ptr->next = n->next;
return n;
}
else {
while(ptr->next != n) {
ptr = ptr->next;
}
ptr->next = n->next;
return n;
}
}
int main(){};
【问题讨论】:
-
你得到的实际错误是什么?
-
始终将错误消息的文本添加到您的问题中。如果这是 Visual Studio,则从“输出”选项卡而不是“错误”选项卡复制文本。
-
方法需要知道它们所属的类。否则它们只是很好的免费函数。
-
@NathanOliver 对此感到抱歉。我编辑了它。生成的 cmets 中存在错误。
-
@drescherjm 我没有 Visual Studio。我正在使用 xcode,但是,感谢您指出这一点。我做了一些 cmets 说明我遇到的错误。
标签: c++ linked-list undeclared-identifier