开发者

Compile errors for double linked list code

开发者 https://www.devze.com 2023-01-08 21:44 出处:网络
I have tried to implement doubly linked list in C++. Here is the code, #include <iostream> using namespace std;

I have tried to implement doubly linked list in C++. Here is the code,

#include <iostream>
using namespace std;
//double linked list
class Link{
    public:
        long data;
        Link *next;

    public:
        Link(long d) {
            data=d;
        }
        void displaylink() {
            cout<<data<<" "<<"\n";
        }
};

class firstl {
    private:
        Link *first;
        Link *last;

    public:
        firstl() {
            first=NULL;
            last=NULL;
        }
    public:
        bool empthy() {
            return (first==NULL);
        }

    public:
        void insertfirst(long dd) {
            Link *newlink=new Link(dd);
            if (empthy)
                last=newlink;
            newlink->next=first;
            first=newlink;
        }

    public :
        void insertlast(long dd) {
            Link *newlink=new Link(dd);
            if (empthy)
                 first=newlink;
            else
                last->next=newlink;
            last=newlink;
        }


    public :
        long deletefirst() {
            long temp=first->data;
            if (first->next==NULL) //if only one item
                last=NULL;//null<-last;
            first=first->next; //first-->old next;
            return temp;
        }
    public:
        void displaylist() {
            Link *current=first;
            while (current!=NULL) {
                current->displaylink();
                current=current->next;
            }
        }
};

int main() {
    firstl linked;
    linked.insertfirst(22);
    linked.insertfirst(44);
    linked.insertfirst(66);
    linked.insertlast(11);
    linked.insertlast(33);
    linked.insertlast(55);
    linked.displaylist();
    linked.deletefirst();
    linked.deletefirst();
    linked.displaylist();
    return 0;
}

But here are the compile errors:

1>------ Build started: Projec开发者_JAVA百科t: linked)list, Configuration: Debug Win32 ------
1>  linked_list.cpp
1>c:\users\david\documents\visual studio 2010\projects\linked)list\linked_list.cpp(40): error C3867: 'firstl::empthy': function call missing argument list; use '&firstl::empthy' to create a pointer to member
1>c:\users\david\documents\visual studio 2010\projects\linked)list\linked_list.cpp(51): error C3867: 'firstl::empthy': function call missing argument list; use '&firstl::empthy' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How can this be fixed?


empthy is a method, therefore an ending () is needed to call it.

void insertfirst(long dd){

    Link *newlink=new Link(dd);
    if (empthy())      // <-----
        last=newlink;
    ...


You have to change

if (empthy)

to

if (empthy())


if (empthy) is wrong, empthy is a function so it should be if (empthy()).BTW, method name should be empty.


empthy is a function, you should call it like this:

if (empthy())


Here's the way I would do it:

#include <list>
typedef std::list<long> firstl;
0

精彩评论

暂无评论...
验证码 换一张
取 消