【问题标题】:Compiler Error: ‘std::array<...>::~array()’ is implicitly deleted编译器错误:'std::array<...>::~array()' 被隐式删除
【发布时间】:2019-07-23 07:46:49
【问题描述】:

我有以下.hpp 文件:

#ifndef CODE_HPP
#define CODE_HPP
#include <array>
#include <vector>
using std::vector;
using std::array;

template<typename T, typename C = vector<T>>
class stack;

template<typename T, typename C = vector<T>, typename K = stack<T,C>>
class stack_array;

template<typename T, typename C>
class stack
{
    C pile;

    stack();
    ~stack();
    void push(T&);
    friend class stack_array<T,C,stack<T,C>>;
};


template<typename T, typename C, typename K>
class stack_array
{
    private:
        static const size_t max_elem = 10;
        array<K, max_elem> store{};
        size_t index;
    public:
        stack_array(T&);
        ~stack_array();
};


template<typename T, typename C> stack<T,C>::stack(){
    pile.clear();
}

template<typename T, typename C> stack<T,C>::~stack(){
}

template<typename T, typename C> void stack<T,C>::push(T& _data){
    pile.push_back(_data);

    return;
}

template<typename T, typename C, typename K> stack_array<T,C,K>::stack_array(T& _data){

    index = 0;

    store[index].push(_data);
}

template<typename T, typename C, typename K> stack_array<T,C,K>::~stack_array(){
}

#endif

我的.cpp文件转载如下:

#include "code.hpp"
#include <iostream>
using std::cout;

int main (void){

    auto i = 0;
    stack_array<decltype(i)> s_a(i);

    return 0;
}

当使用以下命令编译上述代码时:

g++ -ggdb -std=c++17 -Wall main.cpp code.hpp

我收到以下编译错误:

In file included from main.cpp:1:0:
code.hpp: In instantiation of ‘stack_array<T, C, K>::stack_array(T&) [with T = int; C = std::vector<int, std::allocator<int> >; K = stack<int, std::vector<int, std::allocator<int> > >]’:
main.cpp:8:35:   required from here
code.hpp:52:86: error: use of deleted function ‘std::array<stack<int, std::vector<int, std::allocator<int> > >, 10>::~array()’
 template<typename T, typename C, typename K> stack_array<T,C,K>::stack_array(T& _data){
                                                                                      ^
In file included from code.hpp:3:0,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: note: ‘std::array<stack<int, std::vector<int, std::allocator<int> > >, 10>::~array()’ is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~

为什么std::array 的析构函数会因为stack_array&lt;T,C,K&gt;::stack_array()? 中的代码而被调用

【问题讨论】:

  • 这是有意的......堆栈ctor()正在从朋友类stack_array中调用
  • 如果array 析构函数无法访问其中项目的析构函数,您如何期望它工作?更多详情:en.cppreference.com/w/cpp/language/…
  • 假设观察是正确的,,,我摆脱了堆栈类的析构函数,它没有做太多,错误消失了......
  • 如果你没有显式删除析构函数,在这种情况下会有一个隐式定义的(公共)析构函数(这让std::array 很高兴)。如果你显式删除析构函数,你应该得到同样的错误。
  • 我没有使用 =delete 显式删除析构函数;而是让编译器通过完全删除声明和定义来定义堆栈的默认析构函数。这样就消除了错误。

标签: c++ templates compiler-errors


【解决方案1】:

你应该加std::array为好友

template<typename T, typename C>
class stack
{
    C pile;

    stack();
    ~stack();
    void push(T&);
    friend class stack_array<T,C,stack<T,C>>;

    template< class TT, std::size_t N>  friend class std::array ;
};

演示:https://wandbox.org/permlink/TxAyRCvrwj9CtOhV

消息说:std::array&lt;stack&lt;int, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, 10&gt;::~array()’ is implicitly deleted...

所以你有一个std::arraystack&lt;smt&gt;std::array 应该可以删除stack 或者它的析构函数是私有的 => 数组的析构函数被隐式删除。

因此,您必须通过将其设为公开或朋友来使其可供std::array 访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2017-12-02
    • 1970-01-01
    • 2012-12-08
    • 2014-12-28
    • 2014-08-14
    相关资源
    最近更新 更多