c++ - 编译器错误 : ‘std::array<...>::~array()’ is implicitly deleted

标签 c++ templates compiler-errors

我有以下 .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<T,C,K>::stack_array()? 中的代码而被调用

最佳答案

您应该添加 std::array作为 friend

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<stack<int, std::vector<int, std::allocator<int> > >, 10>::~array()’ is implicitly deleted...

所以你有一个 std::arraystack<smt> , std::array应该可以删除 stack或其析构函数是私有(private)的 => 数组的析构函数被隐式删除。

所以你必须让 std::array 可以访问它通过公开或好友。

关于c++ - 编译器错误 : ‘std::array<...>::~array()’ is implicitly deleted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159155/

相关文章:

c++ - 当 T 是左值引用时,模板类方法 f(const T) 不接受右值

c++ - 在不知道参数类型的情况下否定 lambda?

c++ - 内部编译器错误: in decode_addr_const, at varasm.c:2632

C++强制编译时错误基于调用构造函数

c++ - program_options变量映射改变参数

c++ - 使用和不使用 INCLUDED_HEADERNAME_H 定义 header 有什么区别

c++ - Caesar Cipher C++(使用字符指针和移位作为参数)

c++ - 转发 C++0x 中的所有构造函数

c++ - 拆分 std::index_sequence 时出错

bash - Cygwin程序已编译但无法运行