-1

So I was doing a linked list assignment where given two numbers in a linked list form, add the numbers up and make the final answer in a linked list form. I keep getting an "undeclared identifier" error for my code and I was wondering how to fix it.

Error message:

List.cpp:48:3: error: use of undeclared identifier 'append' append(h, c);

Thanks.

 #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;
}

2 Answers2

2

This code:

append(h, c);

calls a free function named append. But there is no such function in your code.

You do have an append function inside the List class, but that's a member function, so you need a List object to call that function on. So you'll need something like:

List l;
l.append(h, c);
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Handy reading to understand some of the terminology used in this answer: [What is the meaning of the term “free function” in C++?](https://stackoverflow.com/questions/4861914/what-is-the-meaning-of-the-term-free-function-in-c) – user4581301 Jun 22 '20 at 22:51
  • @FutureEngineer123 No problem. Consider [accepting](https://stackoverflow.com/help/someone-answers) the answer if it solves your problem. – cigien Jun 23 '20 at 00:31
0

You defined the member function append in the class List (that (the function) by the way does not make any sense)

void List:: append(ListNode *node, vector<char> c) {
//...
}

But inside main you are calling a stand-alone function append

append(h, c);

that is not related to the class List.

You even did nit declare in main an object of the type List.

Pay attention to that the class ListNode has the data member of the type int.

int value;

However you are trying to save in this data member objects of the type char that are element of a vector declared like vector<char> c.

And it is totally unclear why the default constructor of the class ListNode initializes the data member value with the character 'b'.

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

So the code in whole does not make sense.

It seems that you mean that the data member value of the class ListNode had the type char instead of int.

char value;

In this case the function append of the class List can be declared and defined the following way

void List:: append( const std::vector<char> &v ) 
{
    ListNode **current = &head;

    while ( *current ) current = &( *current )->next;

    for ( const auto &item : v )
    {
        *current = new ListNode( item, nullptr );
        current = &( *current )->next; 
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335