Fixing Code to Print "Print": Nodes and Pointers Programming Error

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Nodes Pointers
Click For Summary
SUMMARY

The discussion centers on a programming error in a C++ implementation of a linked list, specifically within the Print method of the LinkedList class. The code fails to compile due to the declaration of an undeclared variable c in the Print method and an incorrect call to head(c) in the main function. The solution involves removing the erroneous variable declaration and correctly iterating through the linked list to print the keys.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with classes and pointers in C++
  • Knowledge of linked list data structures
  • Experience with basic debugging techniques in C++
NEXT STEPS
  • Review C++ class and pointer syntax
  • Learn about linked list traversal and node manipulation
  • Explore error handling and debugging in C++
  • Study best practices for memory management in C++
USEFUL FOR

Software developers, particularly those working with C++ and data structures, as well as students learning about linked lists and debugging techniques.

needOfHelpCMath
Messages
70
Reaction score
0
what is wrong with my programming D: won't print out the "Print"
Code:
 #include <iostream>
#include <cstdlib>
#include<string>

using namespace std;

class Node {   
public:
   Node();
   Node* prev;
   string key;
   Node* next;
   
};

Node::Node() {
   prev = 0;
   next = 0;
   
   return;
}
   

class LinkedList {
public:
  LinkedList();
  void Insert(string key);
  void Print();
  Node* Find(string key);
  void Delete(Node* x);

  Node* head;
};

LinkedList::LinkedList() {
   head = 0;

   
   return;
}
 
 void LinkedList::Insert(string key) {
    Node* n;
    n = new Node;
      n->key = key;
     
      
      // Link new node to current head
      // of list
      n->next = head;
     
      // Make head point to new node
      head = n;
      return;
   }
   
   void LinkedList::Print() {
      Print* c;
      
      return;
   }int main () { 
  LinkedList l;
  Node* n = 0;
  string line;
   
  
    while (true){

     getline(cin, line);
     if (line.empty()){
        break;
     }

     l.Insert(line);
  }

   n = l.head(c);
   
   while (n!=0){
      cout << n->key << endl;
      n = n->next;
   }
   return 0;
}

I want to print out "Print" with the arrow

Code Runs:

Code:
main.cpp: In member function 'void LinkedList::Print()':
main.cpp:58:14: error: 'c' was not declared in this scope
       Print* c;
              ^
main.cpp: In function 'int main()':
main.cpp:80:15: error: 'c' was not declared in this scope
    n = l.head(c);
               ^
main.cpp:80:16: error: expression cannot be used as a function
    n = l.head(c);
^
 
Technology news on Phys.org
The first error message essentially tells you the problem: In "LinkedList.Print()" you have "Print* c" but I see no other mention of "c". Where is "c" defined?
 
HallsofIvy said:
The first error message essentially tells you the problem: In "LinkedList.Print()" you have "Print* c" but I see no other mention of "c". Where is "c" defined?

ahhhh okay i see thank you!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
2K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K