簡體   English   中英

我正在嘗試將我的 header 文件用於雙向鏈表,但是當我在主程序中調用任何函數時,它會給出相同的錯誤代碼

[英]I am trying to use my header files for a doubly linked list but when I call any functions in the main it gives same error code

我已經為雙向鏈表創建了 a.cpp 和 .hpp 文件,但是當我嘗試調用主文件中的任何函數時,我收到錯誤消息

標識符“init”未定義

我不知道我做錯了什么,這是.cpp文件的前幾行

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

這是.hpp文件

#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

最后,這里是主要的 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是 GitList 的成員GitList ,因此需要在GitList的實例上調用。 main中的許多其他 function 調用也是如此。

因此,您的代碼應如下所示:

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();
        }
        ...

此外,您應該擺脫init()並在構造函數(或類的聲明)中初始化指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM