【问题标题】:cannot convert 'this' pointer from 'const List<T>' to 'List<T> &'无法将 'this' 指针从 'const List<T>' 转换为 'List<T> &'
【发布时间】:2021-06-11 20:54:09
【问题描述】:

我实现了一个双向链表。然后我将该链表用作堆栈的容器。但我收到错误 C2662 'const T &List::last_elem(void)': cannot convert 'this' pointer from 'const List' to '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 &amp;top() const,它正在使用last_elem。问题是,在top() 中,所有内容都是const(因为这就是您声明它的方式),但stacklist.last_elem() 尝试在 now-const 成员变量上调用非常量成员函数。这不是编译器中的错误;它是您代码中的一个错误。将last_elem(如果你想要任何一致性,可能还有first_elem)声明为const可能会解决这个问题:即const T &amp;last_elem() const { ..code.. }
  • 成功了。谢谢你的时间。通常我尝试删除 const。我想我错过了。再次感谢
  • g++ 还告诉我 throw(StackException) 的“动态异常规范在 C++11 中已弃用”

标签: c++ data-structures stack


【解决方案1】:

使 last_elem() 成为一个符合以下条件的 const:

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

【讨论】:

  • @OlafDietsche 是的,你是对的,我记得这个词是错误的。改写答案。
  • @dilem-ma-vi 请接受最佳答案并为那些对您有帮助的人点赞。
【解决方案2】:

问题是:

 const T& top() const throw(StackException)

把第二个常量去掉

const T& top() throw(StackException)

你应该没事的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2019-05-20
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多