【问题标题】:Circular linked list with undeclared identifier error带有未声明标识符错误的循环链表
【发布时间】: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


【解决方案1】:

您没有正确限定您的成员函数定义。

它们必须以类名作为前缀,

int CircularLinkedList::addAtFront(Node *n)
{
    // ...
}

您也可能有其他错误。我注意到您的 search 函数与类中的定义不匹配(不同的返回类型)。如果您也有其他问题,我不会感到惊讶。

我现在能给出的最佳建议是阅读错误信息——它试图告诉你一些事情。

【讨论】:

    【解决方案2】:

    几个问题:

    a) 参数头是方法searchaddAtFrontdeleteNode 中未声明的标识符。

    要解决此问题,您可以将类中声明的函数定义为CircularLinkedList::search 而不仅仅是search,并将成员head 引用为this-&gt;head

    b) 你需要定义getLastNode(),它不见了

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2011-10-25
      • 2011-02-15
      • 1970-01-01
      相关资源
      最近更新 更多