C++模板友元类的错误和

发布于 2024-12-19 03:24:37 字数 12030 浏览 0 评论 0原文

我正在创建我自己的双向链表程序(我知道有一个列表库)。我的 main.cc 仅包含我的主函数,它提供了一个菜单来为我的程序选择各种选项并调用正确的函数/对象。然后我有3个不同的包含类文件的头文件,1个是我的双向链表函数的通用文件,另一个是将通用DLL函数转换为队列函数,最后一个是将DLL函数转换为堆栈函数。

main.cc

#include <iostream>


#include "doubly-linked-list.h"
#include "DLLstack.h"
#include "DLLqueue.h"

using namespace std
int choice=0,choice2=0;

int main()
{
    int choice=0,choice2=0,i=0;
    DoublyLinkedList<int> lst;
    DLLstack stlist;
    while(1)
    {

        choice2=0;
        //ask user
        cout<<"\n1. Create Simple (Unsorted) List\n2. Create Sorted List\n";
        cout<<"3. Create LIFO Queue\n4. Create FIFO Queue(Stack)\n";
        cout<<"5. Exit Program\n";
        cout<<"Please choose an option\n";
        cin>>choice;

        while(choice==1)
        {

            //ask user 1.a
            cout<<"\n1. Enter integer for insertion at head of list\n2. Enter integer for insertion at tail of list\n";
            cout<<"3. Display and Delete integer at head\n4. Display and Delete integer at tail\n";
            cout<<"5. Search for integer in list and delete that node\n6.  Display Contents of list from head to tail in order\n7.  Exit\n";
            cout<<"Please choose an option\n";
            cin>>choice2;

            if(choice2==1)//1.1
            {cout<<"Enter integer to add to head\n";
                cin>>i;
                lst.addToDLLHead(i);
            }
            if(choice2==2)//1.2
            {
                cout<<"Enter integer to add to tail\n";
                cin>>i;
                lst.addToDLLTail(i);
            }
            if(choice2==3)//1.3
            { try{
                i=lst.deleteFromDLLHead ();
            } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }

                cout<<"The deleted int was "<<i<<endl;
            }
            if(choice2==4)//1.4
            { try{
                i=lst.deleteFromDLLTail ();
            } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {case 1:
                        cout<<"Empty List\n";
                            return(1);
                    }
                }
                cout<<"The deleted int was "<<i<<endl;
            }
            if(choice2==5)//1.5
            {
                cout<<"Enter Integer to search for and delete"<<endl;
                cin>>i;
                try{
                    lst.searchdelete (i);
                } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }
            }
            if(choice2==6)
            {lst.printlist ();}
            if(choice2==7) choice=0;
        }





        while(choice==2)
        {
            //ask user 2.b
            cout<<"\n1. Enter integer for sorted insertion(increasing order) into list\n2. Display and delete integer if present in list\n";
            cout<<"3. Display contents of sorted list of integers, in increasing order\n";
            cout<<"4. Exit program\n";
            cout<<"Please choose an option\n";
            cin>>choice2;

            if(choice2==1)//2.1
            {cout<<"Enter integer to add to the sorted list"<<endl;
                cin>>i;
                lst.addSorted (i);
            }
            if(choice2==2)
            {
                cout<<"Enter Integer to search for and delete"<<endl;
                cin>>i;
                try{
                    lst.searchdelete (i);
                } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }
            }
            if(choice2==3)
            {lst.printlist ();}
            if(choice2=4)
            {choice=0;}
        }






        while(choice==3)
        {
            cout<<"\n1. ENQUEUE\n2. DEQUEUE\n";
            cout<<"3. Print QUEUE\n";
            cout<<"4. Exit program\n";
            cout<<"Please choose an option\n";
            cin>>choice2;
            DLLQueue.qlst;

            if(choice2==1)
            {
                cout<<"Enter number to place in Queue"<<endl;
                cin>>i;
                qlst.enqueue(i);}
            if(choice2=2)
            {try{qlst.dequeue();
            } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }
            }
            if(choice2=3)
            {lst.printlist ();}
            if(choice2=4)
            {choice=0;}


        }

            while(choice==4)
            {
                cout<<"\n1. Push\n2. Pop\n";
                cout<<"3. Print STACK\n";
                cout<<"4. Exit program\n";
                cout<<"Please choose an option\n";
                cin>>choice2;
                if(choice2==1)
                {cout<<"Please enter value to place in stack"<<endl;
                    cin>>i;
                    stlst.push(i);
                }
                if(choice2==2)
                {stlst.pop();}
                if(choice2==3)
                {lst.printlist ();}
                if(choice2==4)
                {choice=0;}
            }






        } //original while

        return 0;
    }

doubly-linked-list.h

#ifndef DOUBLY_LINKED_LIST
#define DOUBLY_LINKED_LIST
#include <iostream>
using namespace std;

const int EMPTY_LIST=1;
template<class T>
class DLLNode
{
    friend class DoublyLinkedList;
    friend class DLLQueue;
    friend class DLLstack;
public:
    DLLNode(){next=prev=NULL;}
    DLLNode(const T& el, DLLNode *n=NULL, DLLNode *p=NULL)
{info=el;
    next=n;
    prev=p;
}
T info;
DLLNode<T> *next, *prev;
protected:
//T info;
//DLLNode<T> *next, *prev;
private:

};

template<class T>
class DoublyLinkedList
{
    friend class DLLQueue;
    friend class DLLstack;
public:
    DoublyLinkedList() {head=tail=NULL;} //good
    void addToDLLTail(const T&); //good
    T deleteFromDLLTail(); //good
    T isDLLEmpty() {return (head==NULL);} //good
    void addToDLLHead(const T&); //added
    T deleteFromDLLHead(); //added
    void deleteDLLNode(const T&); //added
    bool isInList(const T&) const; //added
    void addSorted(const T&); //added
    void printlist(); //added
    T searchdelete(const T&);


protected:

private:
DLLNode<T> *head, *tail;
};

template<class T>
T DoublyLinkedList<T>::deleteFromDLLTail(){
    if(head!=NULL){
        T el=tail->info;
        if(head==tail){
            delete tail;
            head=tail=NULL;
        }
        return el;
    }
    else throw(EMPTY_LIST);
}

template<class T>
void DoublyLinkedList<T>::addToDLLTail(const T& el) {
    if(tail!=NULL){
        tail=new DLLNode<T>(el,NULL,tail);
        tail->prev->next=tail;
    }
    else head=tail= new DLLNode<T>(el);
}

template<class T>
void DoublyLinkedList<T>::addToDLLHead (const T& el) {
    head = new DLLNode<T>(el,head);
    if(tail==NULL) tail=head;
}

template<class T>
T DoublyLinkedList<T>::deleteFromDLLHead (){
    if(head!=NULL)
    {
        int el=head->info;
        DLLNode<T> *tmp=head;
        if(head==tail)
        {head=tail=NULL;}
        else{head=head->next;}
        delete tmp;
        return(el);
        }
    else throw(EMPTY_LIST);
}

template<class T>
void DoublyLinkedList<T>::deleteDLLNode(const T& el) {  
if(head!=NULL){
   if(head==tail&&el==head->info) {
       delete head;
       head=tail=NULL;
   }
   else if(el==head->info){
       DLLNode<T> *tmp=head;
       head=head->next;
       head->prev=NULL;
       delete tmp;
   }
   else {
       DLLNode<T> *pred, *tmp;
       for(tmp=head->next;tmp!=NULL && tmp->info!=el;tmp=tmp->next);
       if(tmp!=NULL){
           pred=tmp->prev;
           pred->next=tmp->next;
           pred->next->prev=pred;

       if(tmp==tail) {tail=tail->prev;}
           delete tmp;
       }
   }
}
else throw(EMPTY_LIST);
}

template<class T>
bool DoublyLinkedList<T>::isInList(const T& el) const {
    DLLNode<T> *tmp;
    for(tmp=head;tmp!=NULL&&tmp->info !=el; tmp=tmp->next);
    return (tmp !=NULL);
}

template<class T>
void DoublyLinkedList<T>::addSorted(const T& i) {
    DLLNode<T> *tmp, *nxt;
    for(tmp=head;tmp->info<i;tmp=tmp->next);
    nxt=tmp->next;
    tmp->next= new DLLNode<T> (i,nxt,tmp);
    next->prev=tmp->next;
    delete tmp;
    delete nxt;
}

template<class T>
void DoublyLinkedList<T>::printlist() {
    DLLNode<T> *tmp;
    if(head!=NULL){
    for(tmp=head;tmp->next!=NULL;tmp=tmp->next){
        cout<<tmp->info<<endl;
    }
    }
}

template<class T>
T DoublyLinkedList<T>::searchdelete(const T& i)
{ DLLNode<T> *tmp;
    for(;tmp->info!=i&&tmp!=NULL;tmp=tmp->next){}
        delete DLLNode<T> (tmp);
    return(cout<<"Value Deleted from List\n");
}



#endif // DOUBLY_LINKED_LIST

DLLstack.h

#ifndef _DLLSTACK_H_
#define _DLLSTACK_H_
#include <iostream>
using namespace std;

#include "doubly-linked-list.h"
class DLLstack
{
    friend class<class T> DoublyLinkedList;
    friend class<class T> DLLNode;
public:
    DLLstack(){};
    bool isEmpty() const
{return lst.isEmpty();}
    void clear()
{
    while(!list.isEmpty()){
        lst.deleteFromDLLHead();}
}
int pop()
{return (lst.deleteFromHead();}
void push(const int& el);
{lst.addToDLLHead (el);}
int topEl()
{
    int topelement;
    topelement=lst.deleteFromDLLHead ();
    lst.addToDLLHead (topelement);
    return(topelement);
}
protected:

private:
DoublyLinkedList stlst;
};

#endif // _DLLSTACK_H_

DLLqueue.h

#ifndef _DLLQUEUE_H_
#define _DLLQUEUE_H_
#include <iostream>
using namespace std;
#include "doubly-linked-list.h"
template<class T>
class DLLQueue
{
    friend <class T> class DoublyLinkedList
    friend <class T> class DLLNode
public:

DLLQueue(){};
bool isEmpty() const
{ return lst.isEmpty();}
void enqueue(const T& el)
{ lst.addToDLLTail (el);}
T dequeue()
{ return {lst.deleteFromDLLHead ();}
T firstEl()
{
    T front_el;
    front_el=lst.deleteFromDLLHead ();
    lst.addToDLLHead (front_el);
    return(front_el);
}
~DLLQueue() {clear();}
protected:

private:
DoublyLinkedList qlst;
};

#endif // _DLLQUEUE_H_

现在我收到 30 个错误,但我的主要问题(我认为)是它说我的 DLLqueue.h 文件和 DLLstack.h 文件中的友元函数声明是 DLLqueue.h:11:9: 错误: '<' 之前预期有不合格的 id代币 DLLstack.h:11:14: 错误: '<' 之前的预期标识符令牌

以及堆栈和队列类中的第一个对象“未在此范围内声明”

doubly-linked-list.h:141:2: 错误:“下一个”未在此范围内声明

还有更多错误,但是我认为这些是造成主要问题的原因,我需要先解决这些问题才能继续。

如果您想知道我正在 Ubuntu 中的 Ajuta IDE 中编程

Im creating my own doubly linked list program (I know there is a list library). I have my main.cc with just my main function that provides a menu to choose the various options for my program and call the correct functions/objects. I then have 3 different header files containing class files, 1 is the general file for my doubly linked list functions, another is to turn the general DLL functions into queue functions and the last one is to convert the DLL functions to stack functions.

main.cc

#include <iostream>


#include "doubly-linked-list.h"
#include "DLLstack.h"
#include "DLLqueue.h"

using namespace std
int choice=0,choice2=0;

int main()
{
    int choice=0,choice2=0,i=0;
    DoublyLinkedList<int> lst;
    DLLstack stlist;
    while(1)
    {

        choice2=0;
        //ask user
        cout<<"\n1. Create Simple (Unsorted) List\n2. Create Sorted List\n";
        cout<<"3. Create LIFO Queue\n4. Create FIFO Queue(Stack)\n";
        cout<<"5. Exit Program\n";
        cout<<"Please choose an option\n";
        cin>>choice;

        while(choice==1)
        {

            //ask user 1.a
            cout<<"\n1. Enter integer for insertion at head of list\n2. Enter integer for insertion at tail of list\n";
            cout<<"3. Display and Delete integer at head\n4. Display and Delete integer at tail\n";
            cout<<"5. Search for integer in list and delete that node\n6.  Display Contents of list from head to tail in order\n7.  Exit\n";
            cout<<"Please choose an option\n";
            cin>>choice2;

            if(choice2==1)//1.1
            {cout<<"Enter integer to add to head\n";
                cin>>i;
                lst.addToDLLHead(i);
            }
            if(choice2==2)//1.2
            {
                cout<<"Enter integer to add to tail\n";
                cin>>i;
                lst.addToDLLTail(i);
            }
            if(choice2==3)//1.3
            { try{
                i=lst.deleteFromDLLHead ();
            } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }

                cout<<"The deleted int was "<<i<<endl;
            }
            if(choice2==4)//1.4
            { try{
                i=lst.deleteFromDLLTail ();
            } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {case 1:
                        cout<<"Empty List\n";
                            return(1);
                    }
                }
                cout<<"The deleted int was "<<i<<endl;
            }
            if(choice2==5)//1.5
            {
                cout<<"Enter Integer to search for and delete"<<endl;
                cin>>i;
                try{
                    lst.searchdelete (i);
                } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }
            }
            if(choice2==6)
            {lst.printlist ();}
            if(choice2==7) choice=0;
        }





        while(choice==2)
        {
            //ask user 2.b
            cout<<"\n1. Enter integer for sorted insertion(increasing order) into list\n2. Display and delete integer if present in list\n";
            cout<<"3. Display contents of sorted list of integers, in increasing order\n";
            cout<<"4. Exit program\n";
            cout<<"Please choose an option\n";
            cin>>choice2;

            if(choice2==1)//2.1
            {cout<<"Enter integer to add to the sorted list"<<endl;
                cin>>i;
                lst.addSorted (i);
            }
            if(choice2==2)
            {
                cout<<"Enter Integer to search for and delete"<<endl;
                cin>>i;
                try{
                    lst.searchdelete (i);
                } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }
            }
            if(choice2==3)
            {lst.printlist ();}
            if(choice2=4)
            {choice=0;}
        }






        while(choice==3)
        {
            cout<<"\n1. ENQUEUE\n2. DEQUEUE\n";
            cout<<"3. Print QUEUE\n";
            cout<<"4. Exit program\n";
            cout<<"Please choose an option\n";
            cin>>choice2;
            DLLQueue.qlst;

            if(choice2==1)
            {
                cout<<"Enter number to place in Queue"<<endl;
                cin>>i;
                qlst.enqueue(i);}
            if(choice2=2)
            {try{qlst.dequeue();
            } catch(int error_code)
                {
                    cerr<<"Error: "<<error_code<<endl;
                    switch(error_code)
                    {cout<<"Empty List\n";
                            return(1);
                    }
                }
            }
            if(choice2=3)
            {lst.printlist ();}
            if(choice2=4)
            {choice=0;}


        }

            while(choice==4)
            {
                cout<<"\n1. Push\n2. Pop\n";
                cout<<"3. Print STACK\n";
                cout<<"4. Exit program\n";
                cout<<"Please choose an option\n";
                cin>>choice2;
                if(choice2==1)
                {cout<<"Please enter value to place in stack"<<endl;
                    cin>>i;
                    stlst.push(i);
                }
                if(choice2==2)
                {stlst.pop();}
                if(choice2==3)
                {lst.printlist ();}
                if(choice2==4)
                {choice=0;}
            }






        } //original while

        return 0;
    }

doubly-linked-list.h

#ifndef DOUBLY_LINKED_LIST
#define DOUBLY_LINKED_LIST
#include <iostream>
using namespace std;

const int EMPTY_LIST=1;
template<class T>
class DLLNode
{
    friend class DoublyLinkedList;
    friend class DLLQueue;
    friend class DLLstack;
public:
    DLLNode(){next=prev=NULL;}
    DLLNode(const T& el, DLLNode *n=NULL, DLLNode *p=NULL)
{info=el;
    next=n;
    prev=p;
}
T info;
DLLNode<T> *next, *prev;
protected:
//T info;
//DLLNode<T> *next, *prev;
private:

};

template<class T>
class DoublyLinkedList
{
    friend class DLLQueue;
    friend class DLLstack;
public:
    DoublyLinkedList() {head=tail=NULL;} //good
    void addToDLLTail(const T&); //good
    T deleteFromDLLTail(); //good
    T isDLLEmpty() {return (head==NULL);} //good
    void addToDLLHead(const T&); //added
    T deleteFromDLLHead(); //added
    void deleteDLLNode(const T&); //added
    bool isInList(const T&) const; //added
    void addSorted(const T&); //added
    void printlist(); //added
    T searchdelete(const T&);


protected:

private:
DLLNode<T> *head, *tail;
};

template<class T>
T DoublyLinkedList<T>::deleteFromDLLTail(){
    if(head!=NULL){
        T el=tail->info;
        if(head==tail){
            delete tail;
            head=tail=NULL;
        }
        return el;
    }
    else throw(EMPTY_LIST);
}

template<class T>
void DoublyLinkedList<T>::addToDLLTail(const T& el) {
    if(tail!=NULL){
        tail=new DLLNode<T>(el,NULL,tail);
        tail->prev->next=tail;
    }
    else head=tail= new DLLNode<T>(el);
}

template<class T>
void DoublyLinkedList<T>::addToDLLHead (const T& el) {
    head = new DLLNode<T>(el,head);
    if(tail==NULL) tail=head;
}

template<class T>
T DoublyLinkedList<T>::deleteFromDLLHead (){
    if(head!=NULL)
    {
        int el=head->info;
        DLLNode<T> *tmp=head;
        if(head==tail)
        {head=tail=NULL;}
        else{head=head->next;}
        delete tmp;
        return(el);
        }
    else throw(EMPTY_LIST);
}

template<class T>
void DoublyLinkedList<T>::deleteDLLNode(const T& el) {  
if(head!=NULL){
   if(head==tail&&el==head->info) {
       delete head;
       head=tail=NULL;
   }
   else if(el==head->info){
       DLLNode<T> *tmp=head;
       head=head->next;
       head->prev=NULL;
       delete tmp;
   }
   else {
       DLLNode<T> *pred, *tmp;
       for(tmp=head->next;tmp!=NULL && tmp->info!=el;tmp=tmp->next);
       if(tmp!=NULL){
           pred=tmp->prev;
           pred->next=tmp->next;
           pred->next->prev=pred;

       if(tmp==tail) {tail=tail->prev;}
           delete tmp;
       }
   }
}
else throw(EMPTY_LIST);
}

template<class T>
bool DoublyLinkedList<T>::isInList(const T& el) const {
    DLLNode<T> *tmp;
    for(tmp=head;tmp!=NULL&&tmp->info !=el; tmp=tmp->next);
    return (tmp !=NULL);
}

template<class T>
void DoublyLinkedList<T>::addSorted(const T& i) {
    DLLNode<T> *tmp, *nxt;
    for(tmp=head;tmp->info<i;tmp=tmp->next);
    nxt=tmp->next;
    tmp->next= new DLLNode<T> (i,nxt,tmp);
    next->prev=tmp->next;
    delete tmp;
    delete nxt;
}

template<class T>
void DoublyLinkedList<T>::printlist() {
    DLLNode<T> *tmp;
    if(head!=NULL){
    for(tmp=head;tmp->next!=NULL;tmp=tmp->next){
        cout<<tmp->info<<endl;
    }
    }
}

template<class T>
T DoublyLinkedList<T>::searchdelete(const T& i)
{ DLLNode<T> *tmp;
    for(;tmp->info!=i&&tmp!=NULL;tmp=tmp->next){}
        delete DLLNode<T> (tmp);
    return(cout<<"Value Deleted from List\n");
}



#endif // DOUBLY_LINKED_LIST

DLLstack.h

#ifndef _DLLSTACK_H_
#define _DLLSTACK_H_
#include <iostream>
using namespace std;

#include "doubly-linked-list.h"
class DLLstack
{
    friend class<class T> DoublyLinkedList;
    friend class<class T> DLLNode;
public:
    DLLstack(){};
    bool isEmpty() const
{return lst.isEmpty();}
    void clear()
{
    while(!list.isEmpty()){
        lst.deleteFromDLLHead();}
}
int pop()
{return (lst.deleteFromHead();}
void push(const int& el);
{lst.addToDLLHead (el);}
int topEl()
{
    int topelement;
    topelement=lst.deleteFromDLLHead ();
    lst.addToDLLHead (topelement);
    return(topelement);
}
protected:

private:
DoublyLinkedList stlst;
};

#endif // _DLLSTACK_H_

DLLqueue.h

#ifndef _DLLQUEUE_H_
#define _DLLQUEUE_H_
#include <iostream>
using namespace std;
#include "doubly-linked-list.h"
template<class T>
class DLLQueue
{
    friend <class T> class DoublyLinkedList
    friend <class T> class DLLNode
public:

DLLQueue(){};
bool isEmpty() const
{ return lst.isEmpty();}
void enqueue(const T& el)
{ lst.addToDLLTail (el);}
T dequeue()
{ return {lst.deleteFromDLLHead ();}
T firstEl()
{
    T front_el;
    front_el=lst.deleteFromDLLHead ();
    lst.addToDLLHead (front_el);
    return(front_el);
}
~DLLQueue() {clear();}
protected:

private:
DoublyLinkedList qlst;
};

#endif // _DLLQUEUE_H_

Now I get 30 errors, but my main problem (i think) is that it says my friend function declarations in my DLLqueue.h file and DLLstack.h file are
DLLqueue.h:11:9: error: expected unqualified-id before ‘<’ token
DLLstack.h:11:14: error: expected identifier before ‘<’ token

as well as my lst object in both the stack and queue classes "was not declared in this scope"

doubly-linked-list.h:141:2: error: ‘next’ was not declared in this scope

there are more errors but I think these are causing the major problems and i need to fix these first to continue.

If you are wondering I am programming in the Ajuta IDE in Ubuntu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生殊途 2024-12-26 03:24:37
friend <class T> class DoublyLinkedList

应该是

template <class U> friend class DoublyLinkedList;

friend class DoublyLinkedList<T>;

取决于哪些实例实际上需要成为朋友;其他朋友声明也是如此。

DoublyLinkedList qlst;

应该是

DoublyLinkedList<T> qlst;

因为 DoublyLinkedList 是一个模板,而不是一个类型。

DLLStack::push 中的函数体之前不应该有分号:

void push(const int& el);
{lst.addToDLLHead (el);}

( with nomatching ) in >DLLstack::pop

{return (lst.deleteFromDLLHead();}

以及 DLLQueue::deque 中不需要的 {

{ return {lst.deleteFromDLLHead ();}

存在各种拼写错误(例如声明成员 stlst > 并将其称为不同的lstlist) - 编译器错误将直接指向您。

DoublyLinkedList::isDLLEmpty 需要声明为 const,因此可以从其他类的 const 成员函数中调用它(一旦您修复了称为 isEmpty 的拼写错误)。

DoublyLinkedList::searchdelete中,delete DLLNode(tmp)应该只是delete tmp。您还需要修复已删除节点两侧节点中的指针,并返回 T 类型的值(大概是 i),并修复循环,因此它实际上搜索正确的节点(事实上,它迭代到末尾,然后删除空指针,结果什么也不做)。

最后,~DLLQueue 尝试调用不存在的 DoublyLinkedList::clear。您应该实现它或 DoublyLinkedList 的析构函数(或两者)。事实上,该列表存在内存泄漏。它还应该有一个复制构造函数和赋值运算符(如果您不希望该类可复制,只需将其声明为私有)。

另外,您不应使用保留名称(以下划线开头,例如 _DLLQUEUE_H_)作为包含保护。您也不应该将 using namespace std; 放入头文件中;头文件的用户可能不希望全局名称空间被污染。

friend <class T> class DoublyLinkedList

should be

template <class U> friend class DoublyLinkedList;

or

friend class DoublyLinkedList<T>;

depending on which instantiations actually need to be friends; and likewise for the other friend declarations.

DoublyLinkedList qlst;

should be

DoublyLinkedList<T> qlst;

since DoublyLinkedList is a template, not a type.

There's shouldn't be a semicolon before the function body in DLLStack::push:

void push(const int& el);
{lst.addToDLLHead (el);}

You have an unwanted ( with no matching ) in DLLstack::pop:

{return (lst.deleteFromDLLHead();}

And an unwanted { in DLLQueue::deque:

{ return {lst.deleteFromDLLHead ();}

There are various typos (e.g. declaring a member stlst and referring to it variously as lst and list) - the compiler errors will point you directly at these.

DoublyLinkedList::isDLLEmpty needs to be declared const, so it can be called from the const member functions of the other classes (once you've fixed the typos that call it isEmpty).

In DoublyLinkedList::searchdelete, delete DLLNode<T>(tmp) should just be delete tmp. You also need to fix up the pointers in the nodes on either side of the deleted one, and return a value of type T (presumably i), and fix the behaviour of the loop so it actually searches for the correct node (as it is, it iterates through to the end, then deletes the null pointer, with the result that it does nothing).

Finally, ~DLLQueue tries to call DoublyLinkedList::clear which doesn't exist. You should implement either that, or a destructor for DoublyLinkedList (or both). As it is, the list is a memory leak. It should also have a copy constructor and assignment operator (simply declared private if you don't want the class to be copyable).

Also, you shouldn't use reserved names (beginning with an underscore, such as _DLLQUEUE_H_) as include guards. You also shouldn't put using namespace std; in your header files; the user of the header file might not want the global namespace polluted.

゛时过境迁 2024-12-26 03:24:37

将您的朋友声明更改为

template <class U> friend class DLLNode;

此处还缺少分号

class DLLQueue
{
    friend <class T> class DoublyLinkedList
    friend <class T> class DLLNode

Change your friend declaration to

template <class U> friend class DLLNode;

Also semicolons are missing here

class DLLQueue
{
    friend <class T> class DoublyLinkedList
    friend <class T> class DLLNode
笑脸一如从前 2024-12-26 03:24:37
  1. 不要编写自己的列表、堆栈或队列。请使用 std::dequestd::stackstd::queue 适配器。
  2. 永远不要在标头中使用 using namespace std
  3. 您所有的朋友声明都使用错误的语法。它应该是 templatefriend class X; (或用 class 而不是 typename)。另外,标识符区分大小写,DLLStackDLLstack 是两个完全不同的东西。
  4. 您的课程将无法正常进行复印。
  5. 不要抛出整数,而是使用 std::exception 的适当子类。
  6. 您不在成员变量定义中使用模板参数。

我没有看到任何更多的主要问题,但我可能在所有指针内容中遗漏了一些东西。

  1. Don't write your own list, stack or queue. Use std::deque with std::stack and std::queue adaptors instead.
  2. Don't ever do using namespace std in the header.
  3. All your friend declarations use wrong syntax. It should be template <typename T> friend class X; (or with class instead of typename). Also, identifiers are case sensitive, DLLStack and DLLstack are two completely different things.
  4. Your classes will not work properly with copying.
  5. Don't throw ints, use a proper subclass of std::exception instead.
  6. You don't use the template arguments in member variable definitions.

I don't see any more major problems, but I might be missing something in all the pointer stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文