微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将“this”指针从“const List<T>”转换为“List<T> &”

如何解决无法将“this”指针从“const List<T>”转换为“List<T> &”

我实现了一个双向链表。然后我使用该链表作为堆栈的容器。但我收到错误 C2662 'const T &List::last_elem(void)': 无法将 'this' 指针从 '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; 
}

解决方法

问题在于:

 const T& top() const throw(StackException)

删除第二个常量

const T& top() throw(StackException)

你应该没事的。

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

,

使 last_elem() 成为一个常量:

const T &last_elem() const
//                   ^^^^^

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。