How to do linear search using pointers? - c++

I'm still new to programming. I just wanted to ask how do I to do linear search using pointers. I wanted to make a book management program and i have made a program with pointers written.
This is the example of how i want it.
This is the coding
#include <iostream>
#define MAX 5
using namespace std;
struct record
{
int id;//stores id
float price;//store price
int qty;//stores quantity
record* next;//reference to the next node
};
record* head;//create empty record
record* tail;//the end of the record
void push(record *& head, record *&tail, int id, float price, int qty)
{
if (head == NULL)
{
record* r = new record;
r->id = id;
r->price = price;
r->qty = qty;
r->next = NULL;//end of the list
head = r;
tail = r;
}
else if (head != NULL && (MAX - 1))
{
record* r = new record;
r->id = id;
r->price = price;
r->qty = qty;
r->next = head;
head = r;
}
}
int pop(record *&head, record *& tail)
{
if (head == NULL)
{
cout << "No record in memory" << endl;
}
else if (head == tail)
{
cout << "The record "<<"ID: " << head->id << "\nPrice: " << head->price << "\nQuantity: " << head->qty << "\n" << "was deleted" << endl; //CORRECTION HERE
}
else
{
record* delptr = new record;
delptr = head;
head = head->next;
cout << "The record " << delptr->id << ", " << delptr->price << ", " << delptr->qty << " was deleted" << endl; //CORRECTION HERE
delete delptr;
}
return 0;
}
void display(record *&head)
{
record* temp = new record; //CORRECTION HERE
temp = head;
if (temp == NULL)
{
cout << "No record in memory" << endl;
}
else
{
cout << "Record : " << endl;
while (temp != NULL)
{
cout <<"\nID: "<< temp->id << "\nPrice: " << temp->price << "\nQuantity: " << temp->qty <<"\n"<< endl; //CORRECTION HERE
temp = temp->next;
}
}
}
int LinearSearch(record *&head) {
}
char menu()
{
char choice;
cout << "\t::MENU::\n" << endl;
cout << "1. Add new record\n" << endl;
cout << "2. Delete record\n" << endl;
cout << "3. Show record\n" << endl;
cout << "4. Quit\n" << endl;
cout << "-----------------------\n" << endl;
cout << "\nEnter selection : " << endl;
cin >> choice;
return choice;
}
int main()
{
record* head;
record* tail;
head = NULL;
tail = NULL;
char choice;
do
{
cout << "---------------------- - \n" << endl;
choice = menu();
switch (choice) { //CORRECTION HERE
case '1':
int id, qty;
float price;
cout << "Enter ID:";
cin >> id; // Please correct yourself here, what is r here, r is not declared anywhere
cout << "\nEnter Price: ";
cin >> price;
cout << "\nEnter Quantity: ";
cin >> qty;
push(head, tail, id, price, qty);
break;
case '2':
pop(head, tail);
break;
case'3':
display(head);
break;
default:
cout << "Quiting...\n";
}
} while (choice != '4');
return 0;
}
How do I write linear search of pointer code for this coding? I tried finding examples throughout the web and when i execute it, it didn't work so i just leave it blank.

Well, I saw you have a list and you are dealing with pointers.
If you want to do linear search in a record id, for example, you can do it like this:
record *aux = head;
while(aux != NULL){
if(aux->id == id_you_want_to_find){
printf("I found it\n");
}
aux = aux->next;
}
you usually use object.attribute to access the attributes of common objects, but when you have a pointer to an object, you must do pointerToObject->attribute.

You can write one if you want but there is no need to when there already exists libraries that do this for you. Since you are using a list structure I show this using a simple std::list. You could also change this to a std::vector and just do a simple for loop iteration using index notation since the speed of search through them is constant as opposed to linear. Here is one method of doing a search through a list linear.
#include <list>
record* searchRecords( std::list<record>& records, int id ) {
if ( records.empty() ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Invalid list of records: list is empty.";
throw ExceptionHandler( strStream ); // Not Written, but what should be done instead of returning.
return nullptr;
}
std::list<record>::iterator it = records.begin();
while ( it != records.end() ) {
if ( it->id == id ) {
return (&(*it));
}
++it;
}
std::ostringstream strStream;
strStream << __FUNCTION__ << " No entry found in search with ID{" << id << "}.";
Logger::log( strStream, Logger::LOGGER_INFO ); // Not implemented here same as above for ExceptionHandler
return nullptr;
}
Since linked lists are not associative they Must be traversed from beginning to end for every entry N in the list to either insert, find or delete. The time complexity here is linear.
If you want faster time insertion time with large lists you can use <multiset> if there may be duplicate items or <set> if every known item is unique. These have instant insertion. If you want constant search and don't care about insertion time then <vector> is what you would want.

The normal answer would be: don't write a linear search yourself, it's called std::find_if. However, C++ expects that your datastructure exposes iterators. An iterator refers to a record (or the end of your list). You get the actual record by calling operator* on the record, and you get the next record by calling operator++.
Yes, this is similar to pointers. That's intentional; pointers are iterators for contiguous arrays. That means you can call std::find_if on an array. But since you chose to implement a linked list instead of an array, you'd' need to implement your own iterator class.

Related

Segmentation errors(core dumped) after deleting pointers

I am trying to make a mask delivery, ordering service code.
The function order will add a new order to order list.
The function output will output the list from newest to oldest order.
The function deliver removes the oldest order.
The following is the code:
#include <iostream>
#include <string>
using namespace std;
struct Mask {
string type;
string customer;
Mask *next;
};
void order(Mask *&head, string type, string customer){
cout << "Ordering " << type << " for " << customer << endl;
Mask *oldHead = head;
head = new Mask;
head->type = type;
head->customer = customer;
head->next = oldHead;
}
void output(Mask *head){
cout << "Outputting order list " << endl;
for (Mask *p = head; p != NULL; p = p->next)
cout << " " << p->type << " for " << p->customer << endl;
}
void deliver(Mask *&head){
if (head->next == NULL){
cout << "Delivering " << head->type;
cout << " for " << head->customer << endl;
delete head;
}
else
deliver(head->next);
}
int main()
{
Mask *head = NULL;
order(head, "3M-N95", "Alice");
order(head, "OxyAir", "Burce");
order(head, "3M-N95", "Cindy");
output(head);
deliver(head);
output(head);
}
Everything runs smoothly, but it says segmentation error(core dumped) at the end. I tried adding this:
if (head->next->next == NULL){
deliver(head->next);
head->next == NULL;
}
But the problem still exists. Any help is appreciated.
I changed deliver to this:
void deliver(Mask *&head){
if (head->next->next == NULL){
cout << "Delivering " << head->next->type;
cout << " for " << head->next->customer << endl;
head->next = head->next->next;
delete head->next;
}
else
deliver(head->next);
}
Apparently, just setting the pointer to NULL does not fix the problem, so I just updated it so that the second last pointer pointed directly to the end.
in "deliver" you force the function to meet the condition if(head->next == NULL)
and then trying to reach head->next->next which is like trying to say null->next (resulting with segmentation fault).
I would recommend traversing to the last "Mask" object with a while loop instead of using all those "if" statements which lead to the same result, or at least change the second if to else if in order to avoid meeting this "if" again.

I've done a program to display student name and height using queue linked list in C++ now i want to display it in Desceding order i dont know how

using namespace std;
void createsqueue();
void insertname();
void insertheight();
void del();
void push();
void display();
struct nodename
{
string name;
float height;
struct nodename *next;
};
nodename *front;
nodename *rear;
void createstack()
{
front = NULL;
rear = NULL;
}
void insertname()
{
while (true)
{
char dec;
nodename *temp;
temp = new nodename;
std::cout << "ENTER YOUR NAME : ";
std::cin >> temp -> name;
std::cout << "ENTER YOUR HEIGHT : ";
std::cin >> temp -> height;
std::cout <<'\n';
temp -> next = NULL;
if(rear == NULL)
{
rear = temp;
front = temp;
}
else
{
rear -> next = temp;
rear = temp;
}
std::cout << "ADD ANOTHER DATA? (Y/N) : ";
std::cin >> dec;
std::cout <<'\n';
if (dec == 'n' || dec == 'N')
{
break;
}
}
}
void del()
{
if(front != NULL)
{
nodename *temp = front;
cout << "The deleted element is: " << temp -> name << endl;
front = front -> next;
delete temp;
}
else
{
cout << "Queue List is empty!\n";
}
}
void display()
{
nodename *temp = front;
while(temp != NULL)
{
std::cout << "--------------------------" << '\n';
std::cout <<"NAME : "<< temp -> name << endl;
std::cout << showpoint << fixed << setprecision(0);
std::cout <<"HEIGHT : " << temp -> height << endl;
std::cout << showpoint << fixed << setprecision(2);
temp = temp -> next;
std::cout << "--------------------------" << '\n';
}
}
int main()
{
int operation;
createstack();
do
{
std::cout << "\tMAIN MENU" << endl;
std::cout << "1 - ENTER NAME : "
<< "\n2 - DELETE PREV DATA : "
<< "\n3 - DISPLAY DATA : "
<< "\n0 - End Program"
<< "\nEnter your operation: ";
cin >> operation;
switch (operation)
{
case 1: insertname();
break;
case 2: del();
break;
case 3: display();
break;
case 0: cout << "Program End";
break;
default:
cout << "Wrong option. Please insert a new operation: ";
}
}
while(operation ! = 0);
return 0;
}
This is the code.
The program Works fine the only problem I have is displaying the name with height in descending order so that the tallest person's info displays first
I have tried multiple ways but nothing seems to work it might be my in-experience in coding since im a newbie and pardon me for any error im just getting started
Modify the code for inserting a new name such that whenever you add a node,you find the correct position for it before inserting in the linked list.
void insertname(){
cin>>h;
nodename* ctr=front;
while(ctr-> height >h){
ctr=ctr->next;
}
nodename* temp =new nodename;
temp->next=ctr->next
ctr->next=temp
temp->height=h; //similarly for name
}
Just find it if the node added is the first node or any other which you have achieved and the above will add nodes in the descending order, which will be displayed as you wanted. So, you can modify it accordingly.

C++ changing object in vector doesn't work

I want to implement a trie using a vector to store the nodes but somehow my insert method doesn't work. I've managed to build the trie data structure using a different implementation but I would like to understand why my current implementation doesn't work.
Works (not index based storing of childs/references):
struct Trie {
struct Trie *references[26];
bool end; //It is true if node represents end of word.
};
DOESN'T WORK (index based storing of childs/references):
struct node {
int references[26] = {0};
bool end;
};
It doesn't work because of a faulty insert function.
void insert_word(string s){
node *current_node = &trie[0];
// current_node->references[4] = 9999 WORKS! Node in Trie is UPDATED
for(int i=0;i<s.size();i++){
print_trie();
int letter_num = static_cast<int>(tolower(s[i])) - static_cast<int>('a');
int next_index = current_node->references[letter_num];
cout << "letter num: " << letter_num << " next index: " << next_index << endl;
if(next_index == 0){
node new_node;
trie.push_back(new_node);
current_node->references[letter_num] = trie.size()-1; // DOESN'T WORK! Node in Trie is NOT UPDATED
cout << "new value: ";
for(auto c:current_node->references)
cout << c << " ";
cout << endl;
cout << "in for" << endl;
print_trie();
current_node = &trie.back();
} else{
current_node = &trie[next_index];
}
}
current_node->end = true;
}
The problem is that when I access current_node as a reference to an object ob the trie vector and I change its value. The object/node in the trie vector isn't always updated. It works in the second line but further down it somehow stops working. I would like to understand why.
Here is a short debug program I wrote to simplify the problem. Here everything seems to work fine.
n1.references[0] = 1;
n2.references[0] = 2;
n3.references[0] = 3;
trie.push_back(n1);
trie.push_back(n2);
trie.push_back(n3);
node *n = &trie[0];
n->references[0] = 10; // Tree is updated properly
n = &trie[1];
n->references[0] = 11; // Tree is updated properly
Can you help me understand why the insert function doesn't work properly?
EDIT: Minimal working example
#include <vector>
#include <string>
#include <iostream>
using namespace std;
struct node
{
int num_words;
int references [26] = {0};
bool end;
};
vector<node> trie;
int n;
void print_trie(){
cout << "#### NEW PRINT TRIE ##### " << endl;
for(int i=0;i<trie.size();i++){
cout << "node " << i << ": ";
for(int j=0;j<26;j++)
cout << trie[i].references[j] << " ";
cout << endl;
}
}
void insert_word(string s){
node *current_node = &trie[0];
// current_node->references[4] = 9999 WORKS! Node in Trie is UPDATED
for(int i=0;i<s.size();i++){
print_trie();
int letter_num = static_cast<int>(tolower(s[i])) - static_cast<int>('a');
int next_index = current_node->references[letter_num];
cout << "letter num: " << letter_num << " next index: " << next_index << endl;
if(next_index == 0){
node new_node;
trie.push_back(new_node);
current_node->references[letter_num] = trie.size()-1; // DOESN'T WORK! Node in Trie is NOT UPDATED
cout << "new reference value of node: ";
for(auto c:current_node->references)
cout << c << " ";
cout << endl;
current_node = &(trie[trie.size()-1]);
} else{
current_node = &trie[next_index];
}
}
current_node->end = true;
}
int main()
{
node root;
trie.push_back(root);
insert_word("hallohallo");
return 0;
}
Anytime a std::vector<T> undergoes a resizing operation all iterators and pointers to elements are invalidated. Using your mcve as an example of where this goes off the rails, consider the marked lines:
void insert_word(string s){
node *current_node = &trie[0]; // **HERE
for(int i=0;i<s.size();i++){
print_trie();
int letter_num = static_cast<int>(tolower(s[i])) - static_cast<int>('a');
int next_index = current_node->references[letter_num];
cout << "letter num: " << letter_num << " next index: " << next_index << endl;
if(next_index == 0){
node new_node;
trie.push_back(new_node); //** RESIZE
current_node->references[letter_num] = trie.size()-1;
cout << "new reference value of node: ";
for(auto c:current_node->references)
cout << c << " ";
cout << endl;
current_node = &(trie[trie.size()-1]); // **HERE
} else{
current_node = &trie[next_index]; // **HERE
}
}
current_node->end = true;
}
In each location marked with // **HERE, you're storing a pointer to an object hosted in your vector. but the line marked with // **RESIZE can (and will) resize via copy/move/etc the entire vector once the capacity is reached. This means current_node no longer points to a valid object, is a dangling pointer, but your code is none-the-wiser and marches on into undefined behavior.
There are a couple of ways to address this. You could reserve the capacity from inception if you know it ahead of time, but for a more robust solution don't use pointers to begin with. if you enumerate via index instead of pointer your solution becomes the following:
void insert_word(std::string s)
{
size_t idx = 0;
for(int i=0;i<s.size();i++){
print_trie();
int letter_num = static_cast<int>(tolower(s[i])) - static_cast<int>('a');
size_t next_index = trie[idx].references[letter_num];
std::cout << "letter num: " << letter_num << " next index: " << next_index << std::endl;
if(next_index == 0){
trie.emplace_back();
trie[idx].references[letter_num] = trie.size()-1;
std::cout << "new reference value of node: ";
for(auto c : trie[idx].references)
std::cout << c << ' ';
std::cout << std::endl;
idx = trie.size()-1;
} else{
idx = next_index;
}
}
trie[idx].end = true;
}
Notice how all instances of current_node have been replaced with trie[idx]. And changing the "current node" is now just a matter of changing the value of idx, which is relevant even when the underlying vector resizes.
that might be caused by type mismatch int is assigned size_t
try ... = (int)trie.size()-1
#include <vector>
#include <iostream>
using namespace std;
struct node{
int num_words;
int references [26] = {}; //........... int
bool end;
};
vector<node> trie;
int n;
void print_trie(){
cout << "#### NEW PRINT TRIE ##### " << endl;
for(int i=0;i<trie.size();i++){
cout << "node " << i << ": ";
for(int j=0;j<26;j++)
cout << trie[i].references[j] << " ";
cout << endl;
}
}
void insert_word(const string& s){
node *current_node = &trie[0];
// current_node->references[4] = 9999 WORKS! Node in Trie is UPDATED
for(int i=0;i<s.size();i++){
print_trie();
int letter_num = int(tolower(s[i]) - 'a');
int next_index = current_node->references[letter_num];
cout << "letter num: " << letter_num << " next index: " << next_index << endl;
if(next_index == 0){
node new_node;
trie.push_back(new_node);
current_node->references[letter_num] = (int)trie.size()-1; //....size_t DOESN'T WORK! Node in Trie is NOT UPDATED
cout << "new reference value of node: ";
for(auto c:current_node->references)
cout << c << " ";
cout << endl;
current_node = &(trie[trie.size()-1]);
} else{
current_node = &trie[next_index];
}
}
current_node->end = true;
}
int main()
{
node root;
trie.push_back(root);
insert_word("hallohallo");
return 0;
}

segmentation error when using priority queue insert/display functions

I seem to keep get a "Segmentation Error: 11" whenever I run any function(insert and display, unable to tell if delete does it since i can't insert) in the program. I'm not totally sure what it means or where to begin looking to fix it. Any help would be greatly appreciated on where to begin. I understand that it has something to do with the memory and from what I was able to find it could mean that something is taking up too much memory.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "priority_queue.h"
//#include <heap.h>
using namespace std;
struct node{
int priority;
int info;
struct node* link;
};
class PriorityQueue{
private:
node* front;
public:
void Priority_Queue(){
front = 0;
}
void insert(int item, int priority){
node* temp, *q;
temp = new node;
temp->info = item;
temp->priority = priority;
if(front == 0 || priority < front->priority){
temp ->link = front;
front = temp;
}
else{
q = front;
while(q->link != 0 && q->link->priority <= priority)
q = q->link;
temp->link = q->link;
q->link = temp;
}
}
void del(){
node* temp;
if(front == 0)
cout << "Underflow" << endl;
else{
temp = front;
cout << "Delete item is: " << temp->info << endl;
front = front->link;
free(temp);
}
}
void display(){
node* ptr;
ptr = front;
if(front == 0)
cout << "Queue is empty" << endl;
else{
cout << "Queue is: " << endl;
cout << "Priority Item" << endl;
while(ptr != 0){
cout << ptr->priority << endl;
ptr = ptr->link;
}
}
}
};
int main(){
int choice, item, priority;
PriorityQueue pq;
while(1){
cout << "1. Insert" << endl;
cout << "2. Delete" << endl;
cout << "3. Display" << endl;
cout << "4. Quit" << endl;
cout << "Enter Choice " << endl;
cin >> choice;
switch(choice){
case 1:
cout << "Input the item value to be added into the queue" << endl;
cin >> item;
cout << "Enter its priority " << endl;
cin >> priority;
pq.insert(item, priority);
break;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
case 4:
break;
default:
cout << "That is not an option" << endl;
}
}
//while(choice != 4);
return 0;
}
The problem appears to be the class definition. As WhozCraig points out the name of the presumed constructor is wrong, so it never gets called. The corrected code:
class PriorityQueue {
private:
node* front;
public:
PriorityQueue() : front(nullptr) {
}
};
If you had a look in your debugger you'd probably see that front was never properly initialized. In C++ try and use nullptr to represent a "null pointer". In C use NULL. Using 0 creates a lot of ambiguity even if it "works" for historical reasons.

Can't find proper value to use without getting for insert function

I've been coding for a few hours today but seem to have thrown a blank. The assignment is creating a grocery list and adding in certain items to it while checking to see if the item is already in the list, if the list is empty and other things.
While working on the assignment, I can't find the proper value/variable to use in my insert function and it's killing me that something this small has stopped me from doing the assignment. The error is in my GroceryList.cpp file where all the functions are defined and all the code written out so that it removes, inserts, and checks for empty list. Everything else seems to compile just fine so far (I still need to fix some functions) but right now I'm trying to get my insert function to work. Can anyone look over my code and point me in the right direction for which value to use in my insert function? Did I even write the insert function properly? Thanks.
GroceryList.h
#ifndef GROCERYLIST_H
#define GROCERYLIST_H
#include <string>
using namespace std;
namespace CS151GroceryList
{
// node definition
struct ListNode
{
string item;
ListNode *link;
};
// define a type for pointers to nodes
typedef ListNode* ListNodePtr;
// define the GroceryList class
class GroceryList
{
public:
// default constructor initializes an empty list
GroceryList();
// destructor - destroys the list and returns all memory to the heap
~GroceryList();
// returns true if the list is empty; false otherwise
bool empty();
// check to see if an item is in the list. If so, return true. If not, return false.
bool inList(const string& an_item);
// prints all of the items in the list. If insert is implemented correctly, the contents
// of the list will be printed in alphabetical order with no repeats.
void print();
// if an_item is found, remove it from the list
void remove(const string& an_item);
// put a new item into the list. The item should be placed into the correct
// position in the list. If the item is already in the list, no change is
// made to the list. Note, you should put A COPY of an_item into the list
void insert(const string& an_item);
private:
ListNodePtr top;
};
} // end CS151GroceryList namespace
#endif GROCERYLIST_H
GroceryListTest.cpp This test the functions to make sure they work. Adds in specific grocery items to the list, spits out the list, and removes what is in the list. Problem not here
#include <iostream>
#include <string>
#include "GroceryList.h"
using namespace std;
using namespace CS151GroceryList;
int main()
{
GroceryList mylist;
/**********************************************************************
* List insertion test *
**********************************************************************/
cout << "Inserting items into the grocery list." << endl << endl;
mylist.insert("Eggs");
mylist.insert("Bananas");
mylist.insert("Wheat Bread");
mylist.insert("Peanut Butter");
mylist.insert("Milk");
mylist.insert("Apples");
mylist.insert("White Bread");
cout << "Finished inserting items." << endl;
cout << "Grocery list contents:" << endl;
mylist.print();
cout << endl << "Finished printing the list." << endl;
/**********************************************************************
* inList test *
**********************************************************************/
cout << "Testing in list functionality:" << endl;
if (mylist.inList("Oranges"))
{
cout << "Oranges are on the list." << endl;
}
else
{
cout << "Oranges are not on the list." << endl;
}
if (mylist.inList("Apples"))
{
cout << "Apples are on the list." << endl;
}
else
{
cout << "Apples are not on the list." << endl;
}
if (mylist.inList("Peanut Butter"))
{
cout << "Peanut Butter is on the list." << endl;
}
else
{
cout << "Peanut Butter is not on the list." << endl;
}
if (mylist.inList("White Bread"))
{
cout << "White Bread is on the list." << endl;
}
else
{
cout << "White Bread is not on the list." << endl;
}
/**********************************************************************
* List removal test *
**********************************************************************/
cout << "Testing list removal." << endl;
cout << endl << "I decided that I didn't want apples after all. Removing apples." << endl;
mylist.remove("Apples");
cout << "After removing apples, grocery list contents:" << endl;
mylist.print();
cout << endl << "Finished printing the list." << endl << endl;
cout << endl << "I decided that I didn't want white bread either. Removing white bread." << endl;
mylist.remove("White Bread");
cout << "After removing white bread, grocery list contents:" << endl;
mylist.print();
cout << endl << "Finished printing the list." << endl << endl;
cout << endl << "I'm lactose intolerant, I'd better not get milk. Removing milk." << endl;
mylist.remove("Milk");
cout << "After removing milk, grocery list contents:" << endl;
mylist.print();
cout << endl << "Finished printing the list." << endl << endl;
}
GroceryList.cpp The error is in this file
To be exact the error is in this portion of the code. This is where each function gets called and all the inserting, removing, and checking happens. I have used an_item as a place holder so that I know where I need to figure out the proper value for the function. I have tried all of the pointers I currently have and other values. I even created a temp value(not shown here) but that didn't seem to work either.
void GroceryList::insert(const string& an_item)
{
if (top == an_item.insert)
{
return;
}
char next;
while (!empty())
{
delete(top);
}
if (an_item.insert == NULL)
{
top = NULL;
return;
}
ListNodePtr temp = new ListNode;
temp->item = an_item;
temp->link = top;
top = temp;
}
Here's the rest of GroceryList.cpp
#include <iostream>
#include <cstddef>
#include <string>
#include "GroceryList.h"
using namespace std;
using namespace CS151GroceryList;
GroceryList::GroceryList() : top(NULL)
{
//left blank intentionally
}
GroceryList::~GroceryList()
{
while (!empty())
{
/*remove();*/
delete top;
}
}
bool GroceryList::empty()
{
return(top == NULL);
}
void GroceryList::print()
{
ListNode*temp = top;
while (temp != top)
{
cout << temp->item << endl;
temp = temp->link;
}
cout << temp->item << endl;
}
void GroceryList::remove(const string& an_item)
{
if (empty())
{
cout << "Error: remove was attempted on empty list.\n";
exit(1);
}
}
void GroceryList::insert(const string& an_item)
{
if (top == temp.insert)
{
return;
}
char next;
while (!empty())
{
delete(top);
}
if (an_item.insert == NULL)
{
top = NULL;
return;
}
ListNodePtr temp = new ListNode;
temp->item = an_item;
temp->link = top;
top = temp;
}
bool GroceryList::inList(const string& an_item)
{
ListNodePtr temp;
temp = top;
if (temp == an_item.insert)
{
temp = temp->link;
if (temp != an_item.insert)
{
if (temp->link != temp->link)
{
return 1;
}
}
/*cout << "Item is in list. Failed to insert." << endl;
return 1;*/
}
else
{
cout << "Item is not in list. Inserting." << endl;
return 0;
}
}
This is the error I am getting:
error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::insert': function call missing argument list; use '&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::insert' to create a pointer to member
I'm pretty sure it is because I am not using the proper value
You have at least these problems in your code:
1)
void GroceryList::insert(const string& an_item)
{
if (top == temp.insert)
{
return;
}
if temp is local variable, then you can't use it without declaration (and, for best, proper initialization). Besides, "insert" is member function call, you've missed the braces and argument list, like that "temp.insert(smth)"
2)
if (an_item.insert == NULL)
{
top = NULL;
return;
}
the same issue, you should provide arguments to member call insert. But you don't need insert call there, because an_item argument of GroceryList::insert, passed by const reference, so you can use it without NULL checks