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;
}
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);
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;
}
}
Related
I have a LinkedList constructor where I can pass in an array and it builds. Then I can add additional nodes to it by passing in integers.
However, I also want the option to construct the LinkedList, without any arguments. In my LinkedList.h file I've tried to create a constructor that sets the first and last pointers. My add method should construct a Node.
But in my main() function, when I try to use this constructor, I get an error:
request for member ‘add’ in ‘l’, which is of non-class type ‘LinkedList()’
Same error for the other methods called in the main.cpp.
Where am I going wrong in how I structure my two constructors?
main.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
int main()
{
//int A[] {1, 2, 3, 4, 5};
//LinkedList l(A, 5);
LinkedList l();
l.add(8);
l.add(3);
cout << l.getCurrentSize()<<endl;
l.display();
return 0;
}
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
protected:
struct Node
{
int data;
struct Node *next;
};
struct Node *first, *last;
public:
//constructor
LinkedList(){first=nullptr; last=nullptr;}
LinkedList(int A[], int n);
//destructor
virtual ~LinkedList();
//accessors
void display();
virtual int getCurrentSize() const;
virtual bool add(int newEntry);
};
#endif
LinkedList.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
//constructor
LinkedList::LinkedList(int A[], int n)
{
Node *t;
int i = 0;
first = new Node;
first -> data = A[0];
first -> next = nullptr;
last = first;
for(i = 1; i < n; i++) {
t = new Node;
t -> data = A[i];
t -> next = nullptr;
last -> next = t;
last = t;
}
};
//destructor
LinkedList::~LinkedList()
{
Node *p = first;
while (first) {
first = first -> next;
delete p;
p = first;
}
}
void LinkedList::display()
{
Node *p = first;
while(p) {
cout << p -> data << " ";
p = p -> next;
}
cout <<endl;
}
int LinkedList::getCurrentSize() const
{
Node *p = first;
int len = 0;
while(p) {
len++;
p = p -> next;
}
return len;
}
bool LinkedList::add(int newEntry)
{
Node *temporary;
temporary = new Node;
temporary -> data = newEntry;
temporary -> next = nullptr;
if (first==nullptr) {
first = last = temporary;
}
else {
last -> next = temporary;
last = temporary;
}
return true;
}
The problem has nothing to do with your constructors themselves.
LinkedList l(); is a declaration of a function named l that takes no arguments, and returns a LinkedList. That is why the compiler is complaining about l being a non-class type.
To default-construct a variable named l of type LinkedList, drop the parenthesis:
LinkedList l;
Or, in C++11 and later, you can use curly-braces instead:
LinkedList l{};
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm writing a function to find all occurrences of a node in a linked list, the function will return the number of occurrences to the main function which will then display those occurrences. The program does compile but the it just freezes and nothing seems to happen when I enter the correct name to look for, if I enter the wrong name, which is not in the list, the findall function returns 0 and the rest of the program works fine. Please take a look.
main.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "List.h"
void extra(list &);
/***********************************
* Main
* Test function - DO NOT CHANGE
***********************************/
void main()
{
list a;
extra(a);
}
/***********************************
* Extra Credit
* Test function - DO NOT CHANGE
***********************************/
void extra(list &a)
{ int i,n;
node_ptr map[4];
string first,last;
// Find node
cout << endl;
cout << "Enter First and Last name: ";
cin >> first >> last;
n = a.findall(first,last,map,4);
// Display forwards
cout << endl;
cout << "Find List\n--------------\n";
for (i = 0; i < n; i++)
{
map[i]->put(cout);
}
}
List.h
#include "Node.h"
#include <iostream>
#include <string>
using namespace std;
class list
{ public:
list(); // Empty constructor
~list(); // Destructor
int findall(string, string, node_ptr*, int);
node *find(string, string); // Locate a note
private:
node *head;
};
Node.h
#include <iostream>
#include <string>
using namespace std;
class list;
class node
{ friend list;
public:
node(); // Null constructor
~node(); // Destructor
void put(ostream &out); // Put
private:
string first,last;
int age;
node *next;
};
typedef node * node_ptr;
List.cpp
#include "List.h"
#include <iostream>
#include <string>
using namespace std;
/**
* Empty Constructor
*
*/
list::list()
{
head = nullptr;
}
/**
* Destructor Constructor
*
*/
list::~list()
{ if (head == nullptr) return;
node *p = head, *t;
while (p)
{
t = p;
p = p->next;
delete t;
}
head = nullptr;
}
/**
* Locate node
*
*/
node *list::find(string last, string first)
{
node *temp = head;
while (temp)
{
if (temp->first == first && temp->last == last) return temp;
temp = temp->next;
}
return nullptr;
}
/**
* Find all.
*
*/
int list::findall(string first, string last, node_ptr* map, int n)
{
int ans;
ans = 0;
*map = find(first, last);
while (*map != NULL)
{
ans++;
*map = (*map)->next;
*map = find(first, last);
}
return ans;
}
Node.cpp
#include "Node.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/**
* Empty Constructor
*
*/
node::node()
{
last = "";
first = "";
age = 0;
next = nullptr;
}
/**
* Destructor
*
*/
node::~node()
{ if (next != nullptr) next = nullptr;
}
/**
* Put
*
*/
void node::put(ostream &out)
{ out << setw(14) << left << last << setw(14) << first << setw(10) << age << endl;
}
I really appreciate your help. Thank you.
findall() freezes because it gets stuck in an endless loop.
If no matching node is found, the first call to find() returns nullptr and findall() exits.
But if a matching node is found, a loop is entered, calling find() to search the entire list all over again from the beginning. That will find the same node as before. Then you call find() again, and again, and so on.
To solve this issue, if find() returns a matching node, you need to pass the next node in the following call to find() so it can start searching where the previous search left off. Repeat until you reach the end of the list.
class list
{ public:
...
int findall(string first, string last, node_ptr *map, int n);
node_ptr find(string first, string last, node_ptr start = nullptr); // Locate a note
...
};
node_ptr list::find(string last, string first, node_ptr start)
{
node_ptr temp = (start) ? start : head;
while (temp)
{
if ((temp->first == first) && (temp->last == last)) break;
temp = temp->next;
}
return temp;
}
int list::findall(string first, string last, node_ptr* map, int n)
{
int ans = 0;
node_ptr temp = nullptr;
while (ans < n)
{
temp = find(first, last, temp);
if (!temp) break;
*map++ = temp;
++ans;
temp = temp->next;
}
return ans;
}
Update: if you are not able to change the signature of find() then you will have to re-write findall() to duplicate what find() does:
class list
{ public:
...
int findall(string first, string last, node_ptr *map, int n);
node_ptr find(string first, string last); // Locate a node
...
};
node_ptr list::find(string last, string first)
{
node_ptr temp = head;
while (temp)
{
if ((temp->first == first) && (temp->last == last)) break;
temp = temp->next;
}
return temp;
}
int list::findall(string first, string last, node_ptr* map, int n)
{
int ans = 0;
node_ptr temp = head;
while (ans < n)
{
while (temp)
{
if ((temp->first == first) && (temp->last == last)) break;
temp = temp->next;
}
if (!temp) break;
*map++ = temp;
++ans;
temp = temp->next;
}
return ans;
}
So I'm working on a college project where I need to create a linked list of 'Structures'. and when I'm adding a new element to the linked list I get this error, which is weird because I am not even using the stack while doing it.
Here's how 'Structure' is defined:
#ifndef Structure_h
#define Structure_h
#include <stack>
using namespace std;
class Structure
{
public:
int Integer;
stack <int> s;
};
#endif
Definition of node:
#pragma once
#ifndef Node_h
#define Node_h
using namespace std;
#include "Structure.h"
class Node
{
public:
Node();
Structure Str;
Node *next;
};
#endif
LinkedList.h:
#pragma once
#ifndef LinkedList_h
#define LinkedList_h
using namespace std;
#include "Node.h"
class LinkedList
{
public:
LinkedList();
int size;
void add(int a);
Node *Current;
Node *Start;
};
#endif
LinkedList.cpp:
#include "LinkedList.h"
#include <iostream>
LinkedList::LinkedList()
{
Node FirstNode;
Start = Current = &FirstNode;
cout << "Start = " << Start->Str.Integer << endl;
cout << "Current = " << Current->Str.Integer << endl;
}
void LinkedList::add(int a)
{
Node n;
n.Str.Integer = a;
Current->next = &n;
Current = Current->next;
cout << Current->Str.Integer;
}
Now whenever I create a new LinkedList and adding something to it I get this error.
I got a feeling like I'm using the Stack wrong somehow, not sure why though.
Thank you from advance.
In this method:
void LinkedList::add(int a)
{
Node n;
n.Str.Integer = a;
Current->next = &n;
Current = Current->next;
cout << Current->Str.Integer;
}
you are adding as next local variable n, it will get destroyed once add returns. This is undefined behaviour. You should use Node* n = new Node;, dont forget to deallocate.
[edit]
The same applies to other places in your code where you take a pointer to local variable and store it as a list node.
You have:
void LinkedList::add(int a)
{
Node n;
n.Str.Integer = a;
Current->next = &n;
// Here, you are storing a pointer to a local variable.
// The pointer becomes a dangling pointer when the function returns.
Current = Current->next;
cout << Current->Str.Integer;
}
Use a dynamically allocated Node.
void LinkedList::add(int a)
{
Node* n = new Node;
n->Str.Integer = a;
Current->next = n;
Current = Current->next;
cout << Current->Str.Integer;
}
I want to read in student names from a file and insert them into my linked-list, but I am having this problem with an error box. The error reads "Expression: Invalid Null Pointer."
I've googled with no such luck. I think I have an idea where I've went wrong, but I don't know how to fix it.
If you could help, that would be great!
Here is my code:
P.S I'm not nearly done so my code might be incomplete, I'm just trying to weed out all my errors now so I don't have triple my errors at the end.
LList.h
#include <iostream>
#include <iomanip>
#include <string>
#ifndef LLIST_H
#define LLIST_H
typedef int ElementType;
class LList
{
public:
LList();
~LList();
void insert(std::string new_data);
void display();
void remove(std::string delete_data);
private:
class Node
{
public:
std::string data;
Node *next;
Node(std::string data_value = NULL);
};
Node *head;
int mySize;
};
#endif LLIST_H
LList.cpp
#include <iomanip>
#include <iostream>
#include <string>
#include "LList.h"
using namespace std;
LList::Node::Node (string data_value)
{
this -> data = data_value;
this -> next = NULL;
}
LList::LList()
{
this -> head = new Node(0);
mySize = 0;
string data = "";
}
LList::~LList()
{
delete this -> head;
}
void LList::insert(string new_data)
{
Node *tempHolder;
tempHolder = this->head;
while (tempHolder->next != NULL)
tempHolder = tempHolder -> next;
Node *newNode = new Node(new_data);
tempHolder ->next = newNode;
this->mySize++;
}
void LList::display()
{
Node *temp;
temp = head->next;
while(temp -> next != NULL)
{
cout << temp -> data << endl;
temp = temp -> next ;
}
}
void LList::remove(string delete_data)
{
Node *tempHolder;
tempHolder = head;
while (tempHolder->next != NULL )
{
if (tempHolder->next->data == delete_data)
{
Node *delete_ptr = tempHolder->next;
tempHolder->next = tempHolder->next->next;
delete delete_ptr;
mySize-- ;
break;
} else
tempHolder = tempHolder->next;
}
}
Main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "LList.h"
#include <fstream>
using namespace std;
int main()
{
LList student;
ifstream infile;
char readLine[500];
infile.open ("names.txt");
if(infile.is_open())
{
while (!infile.eof())
{
infile.getline(readLine,sizeof(readLine)); // read a line from file
student.insert(readLine);
}
}
else
{
cout << "Can't open file!" << endl;
}
}
I found my problem.
In:
LList::LList()
{
this -> head = new Node(0);
mySize = 0;
string data = "";
}
Node(0);
is calling my
LList::Node::Node (string data_value)
{
this -> data = data_value;
this -> next = NULL;
}
which is initialized as a string.
I changed
Node(0);
to
Node("");
and it worked flawlessly.
I wonder could you give the reference where you read that you may to write?
Node(std::string data_value = NULL);
Class std::string has no constructor that converts NULL to an object of type std::string.
it would be much better to declare the constructor without a default argument
Node( std::string data_value );
There is no any sense to create a node without data.
In fact there is no any need to declare a constructor of Node. It could be used as an aggregate.
Also change the constructor of LList as
LList::LList() : head( 0 ), mySize( 0 ){}
Also the destructor is invalied
LList::~LList()
{
delete this -> head;
}
You have to delete not only head but all nodes in the LList.
Also nodes in a single linked list are inserted in the beginning of the list that is before the head.
I would write method insert the following way provided that the constructor of Node is removed bacause it is not needed.
void LList::insert( const std::string &new_data )
{
head = new Node { new_data, head };
}
If your compiler does not support the initializer list then you indeed need to define constructor in class Node.
Node( const std::string &data_value, next_node = NULL );
In this case method insert will look as
void LList::insert( const std::string &new_data )
{
head = new Node( new_data, head );
}
Read integers into a simple linked list from text file. Then bubblesort the list of integers and read out to another file. Right now I am reading into the main but I am trying to overload the extraction operator to read it in and I am not sure how to go about that. My Bubblesort function is also causing alot of issues. Its telling me the function cannot be overloaded and the node identifier is undeclared amongst other things. Any help would be greatly appreciated
Main file
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include "bubble.h"
using namespace std;
struct nodeType
{
int info;
nodeType* link;
};
node *head_ptr = NULL;
void Display();
void list_clear(nodeType*& head_ptr);
void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr);
Bubblesort();
int main()
{
ifstream datld;
ofstream outld;
Bubble D3;
datld.open ("infile2.txt");
if (!datld)
{
cout << "failure to open data.txt" << endl;
system ("pause");
return 1;
}
datld >> D3;
while(datld)
{
cout << D3<< endl;
datld >> D3;
}
system("pause");
return 0;
Bubblesort();
}
void Bubblesort()
{
node* curr = head_ptr;
int count = 0;
while(curr!=NULL)
{
count++;
curr = curr->NEXT;
}
for(int i = count ; i > 1 ; i-- )
{
node *temp, *swap1;
swap1 = HEAD;
for(int j = 0 ; j < count-1 ; j++ )
{
if(swap1->DATA > swap1->NEXT->DATA)
{
node *swap2 = swap1->NEXT;
swap1->NEXT = swap2->NEXT;
swap2->NEXT = swap1;
if(swap1 == HEAD)
{
HEAD = swap2;
swap1 = swap2;
}
else
{
swap1 = swap2;
temp->NEXT = swap2;
}
}
temp = swap1;
swap1 = swap1->NEXT;
}
}
}
void list_clear(nodeType*& head_ptr)
//Library facilities used:cstdlib
{
nodeType * removeptr;
while(head_ptr!=NULL)
{
removeptr=head_ptr;
head_ptr=head_ptr->link;
delete removeptr;
}
}
void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr)
{
nodeType* temp;// to allocate new nodes
head_ptr=NULL;
tail_ptr=NULL;
if(source_ptr==NULL)
return;
head_ptr=new nodeType;
head_ptr->link=NULL;
head_ptr->info=source_ptr->info;
tail_ptr=head_ptr;
source_ptr=source_ptr->link;
while(source_ptr!=NULL)
{
temp = new nodeType;
temp->link=NULL;
temp->info =source_ptr-> info;
tail_ptr->link=temp;
tail_ptr = tail_ptr->link;
source_ptr = source_ptr->link;
}
}
Header file
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class Bubble
{
private:
int manynodes;
public:
Bubble() { }
void Bubblesort();
friend ostream &operator<<( ostream &output, const Bubble &D )
{
output << D.manynodes;
return output;
}
friend istream &operator>>( istream &input, Bubble &D )
{
input >> D.manynodes;
return input;
}
};
Your extraction operator looks fine. I don't know if it really does what you need, but that's another issue.
You are declaring function Bubblesort() twice: first in the header file as void Bubblesort(), then in the main file as just Bubblesort() (this should at least give you a warning that it is considered to mean int Bubblesort()). You cannot overload a function just on the return value, hence the error.
Indeed, you are using a type called node in several places, but you have not declared nor defined it anywhere.