首页 > 解决方案 > 无法从“常量列表”转换“this”指针'到'列表&'

问题描述

我实现了一个双向链表。然后我将该链表用作堆栈的容器。但我收到错误 C2662 'const T &List::last_elem(void)': cannot convert 'this' pointer from 'const List' 到 'List &'。我试图作为一个值返回,但它没有用。我不明白编译器是否指向错误点。

template<typename T>
struct Node
{
    T data;
    Node* next;
    Node* prev;
    Node() :data{ 0 } { next = nullptr; prev = nullptr; }
};
template<typename T>
class List
{
private:
    Node<T>* headNode;
    Node<T>* tailNode;
public:
    List()
    {
        headNode = new Node<T>;
        tailNode = new Node<T>;
        headNode->next = tailNode;
        tailNode->prev = headNode;
    }
    ~List()
    {
        Node<T>* current = headNode;
        while (current)
        {
            Node<T>* tempNode = current;
            current = current->next;
            delete tempNode; cout << "\nDeleting List!!!";
        }
    }
    bool empty()
    {
        return (headNode->next == tailNode);
    }
    const T &last_elem()
    {
        return tailNode->prev->data;
    } 
    const T &first_elem()
    {
        return headNode->next->data;
    }
    void remove_first()
    {
        Node<T>* tempNode = headNode;
        headNode = tempNode->next;
        delete tempNode; cout << "\nDeleting Node!!!";
        headNode->prev = nullptr;
    }
    void remove_last()
    {
        Node<T>* tempNode = tailNode;
        tailNode = tempNode->prev;
        delete tempNode; cout << "\nDeleting Node!!!";
        tailNode->next = nullptr;
    }
    void add_front(T d)
    {
        headNode->data = d;
        Node<T>* tempNode = new Node<T>;
        tempNode->next = headNode;
        headNode->prev = tempNode;
        headNode = tempNode;

    }
    void add_end(T d)
    {
        tailNode->data = d;
        Node<T>* tempNode = new Node<T>;
        tempNode->prev = tailNode;
        tailNode->next = tempNode;
        tailNode = tempNode;
    }
    void print_list()
    {
        Node<T>* current = headNode->next;
        while (current)
        {
            cout << current->data << "|-> ";
            current = current->next;
        }
    }
    void reverse_print_list()
    {
        Node<T>* current = tailNode->prev;
        while (current)
        {
            cout << current->data << "|-> ";
            current = current->prev;
        }
    }
};
template<typename T>
class ListStack
{
private: 
    List<T> stacklist; 
    int index; 
public: 
    class StackException
    {
    private:
        string errMessage;
    public:
        StackException(string err) :errMessage(err) {}
        string getErrMessage() { return errMessage; }
    };
    ListStack():index { -1 }{}
    int size() const // number of items in the stack
    {
        return index + 1;
    }
    bool empty() const // is the stack empty? 
    {
        return (index == -1);
    }
     const T& top() const throw(StackException) // the top element 
    {
        if (empty())throw StackException(string("Stack is empty!!!"));
        return stacklist.last_elem();
    }
    void push(const T& e) // push element onto stack 
    {
        stacklist.add_end(e); 
        ++index;
    }
    void pop() throw(StackException) // pop the stack 
    {
        if (empty())throw StackException(string("Stack is empty!!!"));
        stacklist.remove_last();
        --index;
    }
};

int main()
{
    try
    {
        ListStack<int> myls;
        myls.push(5);
        myls.push(8);
        myls.push(9);
        myls.push(12);
        myls.push(17);
        cout << myls.top() << endl;
        myls.pop();
        myls.pop();
        myls.pop();
        myls.pop();
        myls.pop();
        myls.pop();
        myls.pop();
        myls.pop();
        myls.pop();
    }
    catch (ListStack<int>::StackException se)
    {
        cout << se.getErrMessage();
    }


    return 0; 
}

标签: c++data-structuresstack

解决方案


问题是:

 const T& top() const throw(StackException)

删除第二个 const 到

const T& top() throw(StackException)

你应该没事。

(这是因为被调用的last_elem()方法不是 const ,因为常量方法显然不能调用非常量方法,所以使一个 const 也可以工作)。


推荐阅读