c++ - 摆脱 “Undeclared identifier”错误

标签 c++ compiler-errors linked-list undeclared-identifier

因此,我正在做一个链表分配,其中以链表形式给定两个数字,将这些数字加起来并以链表形式给出最终答案。我的代码不断出现“未声明的标识符”错误,我想知道如何解决它。
错误信息:
List.cpp:48:3:错误:使用未声明的标识符“追加”
append(h,c);
谢谢。

 #include <iostream>
    
#ifndef LISTNODE_H
#define LISTNODE_H

using namespace std;

class ListNode {
 public:
  ListNode();
  ListNode(int value, ListNode* next);
  int value;
  ListNode *next;
  
 private:
  friend class List;
};

#endif







#include <iostream>
#include "ListNode.h"

using namespace std;

ListNode:: ListNode() {
  value = 'b';
  next = NULL;
}





#include <iostream>
#include "ListNode.h"
#include <vector>

#ifndef LIST_H
#define LIST_H

using namespace std;

class List {
 public:
  List();
  void append(ListNode* node, vector<char> c);
  ListNode *head;
  
};

#endif








#include <iostream>
#include <string>
#include "List.h"
#include <vector>
#include "ListNode.h"

using namespace std;

void List:: append(ListNode *node, vector<char> c) {
  //ListNode *temp
  for(int i = 0; i < c.size(); i++) {
    if(head == NULL) {
      head = node;
    }

    else {
      ListNode* itr = head;
      while(itr -> next != NULL) {
    itr = itr -> next;
      }
      node = itr -> next;
      node -> value = c[i];
      cout << node -> value << endl;
    }
  }
}


List:: List() { //Initializes the head and the tail for the whole class
  head = NULL;
}

int main() {
  ListNode *h;
  string num1, num2, sentence;
  vector<char> c;
  cout << "Type in two numbers" << endl;
  cout << "Number 1: " << endl;
  cin >> num1;
  cout << "Number 2: " << endl;
  cin >> num2;
  //cout << "Type in a sentence: " << endl;
  //cin >> sentence;
  cout << "--------" << endl;
  for(int i = 0; i < num1.size(); i++) {
    c.push_back(num1[i]);
  }
  append(h, c);
  return 0;
}

最佳答案

这段代码:

append(h, c);
调用一个名为append的自由函数。但是您的代码中没有这样的功能。
append类中确实有一个List函数,但这是一个成员函数,因此您需要一个List对象来调用该函数。因此,您将需要以下内容:
List l;
l.append(h, c);

关于c++ - 摆脱 “Undeclared identifier”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62524706/

相关文章:

c++ - 在字符串的 std::map 中查找编译错误,长

java - 一个链表打印,但另一个非空链表不打印 : Revised and expanded

asp.net - 将 ASP.NET 应用程序拆分为两个应用程序 - 处理共享页面/用户控件/脚本

c++ - 使用 VideoWriter_GPU 时如何解决 OpenCV 错误“函数未实现(调用的功能对于当前构建或平台已禁用)?

c# - 该名称在当前上下文中不存在

c++ - 为什么 boost::program_options 接受切词?

java - 如何在另一个类的一个类中调用方法?

c - 交换链表中的节点?

c++ - TBB 并发无序映射导致段错误

c++ - 获取多维可变参数 std::array 的根类型