【问题标题】:Use of deleted function?使用删除功能?
【发布时间】:2020-01-20 22:22:57
【问题描述】:

这是我的主要内容:

#include <iostream>
#include "celle.h"
using namespace std;
int main(){
    cell a(1,9,true);
    grid A;
    return 0;}

我正在尝试实现一个单元列表,但是编译器向我报告了这个错误:

|10|错误:没有匹配的函数调用'cell::cell()'|

|52|错误:使用已删除的函数'List_node::List_node()'|

|139|错误:使用已删除的函数'List_node::List_node()'|

linked_list.h 代码:

    template <class T>
    class Linked_list;
    
    template <class T>
    class List_node{
        friend class Linked_list<T>;
     private:
        T _value;
        List_node<T> * _pPrev;
        List_node<T> * _pNext;
    };
    
    template <class T >
    class Linked_list : public Linear_list<T, List_node<T>*>{
     public:
        typedef typename Linear_list<T, List_node<T>*>::value_type value_type;
        typedef typename Linear_list<T, List_node<T>*>::position position;
    
        Linked_list();
        ~Linked_list();
        void create();
        bool empty() const;
        value_type read(position) const;
        void write(const value_type &, position);
        position begin() const;
        void erase(position);
        void insert(const value_type &, position);
        };
    
     private:
        List_node<T> * _pHead;
        int _length;
    };
    
    template <class T>
    Linked_list< T >::Linked_list() {
        _pHead = new List_node<T>;
        _pHead->_pNext = _pHead;
        _pHead->_pPrev = _pHead;
        _length = 0;
    }
    
    template< class T >
    Linked_list< T >::~Linked_list(){
        while(!empty())
            erase(begin());
        delete _pHead;
    }
    
    template< class T >
    void Linked_list< T >::create(){
        if (empty())
                _length = 0;
    }
    
    template< class T >
    bool Linked_list< T >::empty() const {
        return(_pHead == _pHead->_pNext);
    }
    
    template< class T >
    typename Linked_list< T >::position Linked_list< T >::begin() const {
        return (_pHead->_pNext);
    }
    
    template< class T >
    typename Linked_list< T >::value_type
    Linked_list< T >::read(position p) const {
        if (!end(p))
            return(p->_value);
    }
    
  template< class T >
  void Linked_list< T >::write(const value_type &a, position p) {
        if (!end(p))
        p->_value = a;
    }
    
  template< class T >
  void Linked_list< T >::insert(const value_type &a, position p){
        position t = new List_node<T>;
        t->_value = a;
        t->_pNext = p;
        t->_pPrev = p->_pPrev;
        p->_pPrev->_pNext = t;
        p->_pPrev = t;
        _length++;
    }

template< class T >
void Linked_list< T >::erase(position p){
    if (!empty() && !end(p)){
        p->_pPrev->_pNext = p->_pNext;
        p->_pNext->_pPrev = p->_pPrev;
        delete p;
    }
}

celle.h 代码:

#include "linked_list.h"

using std::cout;
using std::endl;
using std::cin;

class grid;

class cell{
public:
    friend class grid;
    cell(int x,int y, bool viva){
        this->x=x;
        this->y=y;
        this->viva=viva;
    }
    ~cell(){};

private:
    int x;
    int y;
    bool viva;
};

class grid{
public:
    grid();
    grid(int,int);
    ~grid(){};
    void insert (cell &);
    void remove (cell &);
    void moveLeft (int x, int y);
    void moveRight (int x, int y);
    void moveDown (int x, int y);
    void moveUpper (int x, int y);
    bool Sorrounded (int x, int y);
    void RemoveSorrounded ();

private:
    Linked_list<cell> cells;
    int rig;
    int col;
};

grid::grid(){
    rig=5;
    col=5;
}

grid::grid(int r, int c){
    rig=r;
    col=c;
}

/*void grid::insert (cell &k){
    if(k.viva){
        cells.insert(k,cells.begin());
    }
}*/

我该如何解决?为什么告诉我节点构造函数被隐式删除了? 我快疯了,对不起,如果我把整个代码复制给你了

【问题讨论】:

  • 编译器只在你不声明构造函数时才创建默认构造函数。当 cell 被构造但没有提供参数时,你想发生什么?在这种情况下你想如何初始化成员?
  • 你检查this one

标签: c++ constructor


【解决方案1】:

当您创建 Linked_list 类型为 cell 时,这意味着 List_node 具有类型 cell。在您的List_node 中,您有T _value;,它存储cell 的值。但是,cell 没有默认构造函数,因为您将其构造函数定义为 cell(int,int, bool)。这会隐式删除List_node 中的默认构造函数。在您的insert 方法中,对于您的Linked_list,您执行position t = new List_node&lt;T&gt;;,它调用其默认构造函数,该构造函数不存在。

解决此问题的一种简单方法是使用List_node 构造函数,该构造函数通过引用类型T 获取值,该构造函数通过复制所需的T 用于构造函数。像这样的:

List_node( const T& value ) : _value(value) {}

然后在您的插入方法中,您将拥有:

position t = new List_node<T>( a );
// t->_value = a; // not needed anymore

更复杂的方法是使用forward_args 进行就地构建。但在您的代码中,一个简单的副本就足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2015-05-02
    • 2010-11-19
    相关资源
    最近更新 更多