I have created a .cpp and .hpp file for a doubly linked list but when I try to call any functions in the main file i get the error message
identifier "init" is undefined
I don't know what I am doing incorrectly, here is the first few lines of the .cpp file
#include<iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include "git.hpp"
using namespace std;
void GitList::init()
{
head=NULL;
tail=NULL;
}
void GitList::insertFirst(int element)
{
struct node* newItem;
newItem=new node;
if(head==NULL)
{
head=newItem;
newItem->prev=NULL;
newItem->value=element;
newItem->next=NULL;
tail=newItem;
}
else
{
newItem->next=head;
newItem->value=element;
newItem->prev=NULL;
head->prev=newItem;
head=newItem;
}
}
here is the .hpp file
#ifndef GIT_HPP
#define GIT_HPP
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <sstream>
using namespace std;
struct node
{
int value;
struct node* next;
struct node* prev;
};
// struct node* head;
// struct node* tail;
class GitList
{
private:
struct node* head;
struct node* tail;
public:
void init();
void insertFirst(int element);
void insertLast(int element);
void insertAfter(int old, int element);
void deleteFirst();
void deleteLast();
void deleteItem(int element);
struct node* searchItem(int element);
void printList();
int dltfrst();
int dltlast();
};
#endif
finally, here is the main function
#include "git.hpp"
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <sstream>
using namespace std;
int main()
{
// int menuSelect = 0;
// while(menuSelect != 5)
// {
// cout << "Select an option:" << endl;
// cout << "1. Add files to current commit" << endl;
// cout << "2. Remove files from current commit" << endl;
// cout << "3. Commit changes" << endl;
// cout << "4. Input commit number to view version" << endl;
// cout << "5. Quit" << endl;
// cin >> menuSelect;
// switch(menuSelect)
// {
// case 1:
// {}
// case 2:
// {}
// case 3:
// {}
// case 4:
// {}
// case 5:
// {}
// default:
// {}
// }
//}
init();
int choice;
while(1)
{
printf("1.InsertFirst 2. InsertLast 3. InsertAfter 4.DeleteFirst 5. DeleteLast\n");
printf("6.SearchItem 7. PrintList 8. ReversePrint 9. DeleteItem \n");
printf("10. Left Rotate 11. Right Rotate 12. Exit 13.Make reverse\n");
cin>>choice;
if(choice==1)
{
int element;
cout<<"Enter element_";
cin>>element;
insertFirst(element);
printList();
}
else if(choice==2)
{
int element;
cout<<"Enter element_";
cin>>element;
insertLast(element);
printList();
}
else if(choice==3)
{
int old,newitem;
cout<<"Enter Old Item_";
cin>>old;
cout<<"Enter new Item_";
cin>>newitem;
insertAfter(old,newitem);
printList();
}
else if(choice==4)
{
deleteFirst();
printList();
}
else if(choice==5)
{
deleteLast();
printList();
}
else if(choice==6)
{
int item;
cout<<"Enter Item to Search_";
cin>>item;
struct node* ans=searchItem(item);
if(ans!=NULL) cout<<"FOUND "<<ans->value<<endl;
else cout<<"NOT FOUND"<<endl;
}
else if(choice==7)
{
printList();
}
else if(choice==8)
{
printReverse();
}
else if(choice==9)
{
int element;
cin>>element;
deleteItem(element);
printList();
}
else if(choice==10)
{
int x;
cin>>x;
leftRotate(x);
printList();
}
else if(choice==11)
{
int x;
cin>>x;
rightRotate(x);
printList();
}
else if(choice==12)
{
break;
}
else if(choice==13)
{
makereverse();
}
}
return 0;
}
init
is a member function of GitList
and therefore needs to be called on an instance of GitList
. The same is true of a number of other function calls in main
.
So, your code should look like this:
int main()
{
GitList gitlist;
gitlist.init();
int choice;
while(1)
{
printf("1.InsertFirst 2. InsertLast 3. InsertAfter 4.DeleteFirst 5. DeleteLast\n");
printf("6.SearchItem 7. PrintList 8. ReversePrint 9. DeleteItem \n");
printf("10. Left Rotate 11. Right Rotate 12. Exit 13.Make reverse\n");
cin>>choice;
if(choice==1)
{
int element;
cout<<"Enter element_";
cin>>element;
gitlist.insertFirst(element);
gitlist.printList();
}
...
Also, you should get rid of init()
and initialise your pointers in the constructor (or in the declaration of the class).