繁体   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