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

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我已经为双向链表创建了 a.cpp 和 .hpp 文件,但是当我尝试调用主文件中的任何函数时,我收到错误消息

identifier "init" is undefined标识符“init”未定义

I don't know what I am doing incorrectly, here is the first few lines of the.cpp file我不知道我做错了什么,这是.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;
    }
}

here is the.hpp file这是.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

finally, here is the main function最后,这里是主要的 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 . init是 GitList 的成员GitList ,因此需要在GitList的实例上调用。 The same is true of a number of other function calls in main . main中的许多其他 function 调用也是如此。

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).此外,您应该摆脱init()并在构造函数(或类的声明)中初始化指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我试图在双循环链表之间输入一个节点时,它给出了错误的 output - When I am trying to enter a node between doubly circular linked list it gives wrong output 当我将双向链表的头部分配给我创建的新节点时出现分段错误 - I am getting a segmentation error when I assign the head of my doubly linked list to a new node I created 双链表 - 导致我的代码抛出编译错误的原因是什么?如何解决? - Doubly Linked List - What is causing my code to throw a compiler error and how can I fix it? 我正在创建一个双向链接列表,该列表将按字母顺序排列一个名称列表,但是我不确定要在int main()函数中放入什么 - I am creating a doubly linked list that will alphabetize a list of names, but I am unsure of what to put in the int main( ) function 当包含标题时,为什么在代码中出现“使用未定义类型”错误? - Why am I getting an “use of undefined type” error in my code, when I have the header for it included? 对双向链表进行排序会导致运行时错误 - Sorting doubly linked list gives a runtime error 尝试在双向链表的中间插入新节点时是否接线错误? - Have i miswired when trying to insert a new node at the middle of doubly linked list? 当我尝试在 c++ 中实现我的链表时出现奇怪的行为 - Strange behaviour when I am trying to implement my linked list in c++ 当包含加载程序标头时,为什么不能使用OpenGL函数? - Why am I not able to use OpenGL functions when I include my loader header? 当我从我的 header 和实现文件调用我的 function 到我的主文件时,我没有得到任何 output - When I call my function from my header and implementation file to my main file, I do not get any output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM