类模板实例化错误

时间:2019-07-08 09:30:10

标签: c++ compiler-errors template-classes objectinstantiation

下面的.hpp文件编译时没有任何错误或警告:

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


//stack template definition
template<typename T, typename C = vector<T>>
class stack{
    C pile;
    static const size_t max_sz = 10;
    stack();
    ~stack();
    void push (T&);
    T& pop (void);
    size_t size (void);
    bool is_full (void);
    friend class stack_array;
};

//stack_array template definition
template<typename T, typename C = vector<T>, typename K = stack<T, C>>
class stack_array{
    private:
        static const size_t max_elem = 10;
        array<K, max_elem> store;
        size_t curr_idx;

        bool is_full (void);

    public:
       stack_array(T&);
       ~stack_array();
       void push (T&);
       T& pop (void);
};

//stack methods
//need a default constructor to define an array of stack objects
template<typename T, typename C> stack<T,C>::stack(){
}

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>T& stack<T,C>::pop(void){
    return(pile.pop_back());
}

template<typename T, typename C> size_t stack<T,C>::size(void){
    return(pile.size());
}

template<typename T, typename C> bool stack<T,C>::is_full(void){
    return(pile.size() == max_sz);
}
//stack_array methods
//template<typename T, typename C = vector<T>, typename K = stack<T, C>>
template<typename T, typename C, typename K>
stack_array<T,C,K>::stack_array(T& _data){
    curr_idx = 0;
    store[curr_idx].push(_data);
}

template<typename T, typename C, typename K>
//template<typename T, typename C = vector<T>, typename K = stack<T, C>>
stack_array<T,C,K>::~stack_array(){
}

template<typename T, typename C, typename K>
//template<typename T, typename C = vector<T>, typename K = stack<T, C>>
void stack_array<T,C,K>::push(T& _data){
    if(!store[curr_idx].is_full()){
        store[curr_idx].push(_data);
    }
    else{
            if(!is_full())
            {
                ++curr_idx;
                store[curr_idx].push(_data);
            }
    }
    return;
}

template<typename T, typename C, typename K>
T& stack_array<T,C,K>::pop(void){
    //should return the last pushed entry
    return(store[curr_idx].pop());
}

template<typename T, typename C, typename K>
bool stack_array<T,C,K>::is_full(void){
    return(store.size() == max_elem);
}

#endif

但是如果main()中仅有两行未注释:

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

    int main (void){

        //string s = "";
        //stack_array<string> s_a(s);

        return 0;
    }

一切都破灭了,我得到了很多错误:

code.hpp: In instantiation of ‘class stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >’:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:110:56:   required from ‘struct std::array<stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, 10>’
code.hpp:28:28:   required from ‘class stack_array<std::basic_string<char> >’
main.cpp:11:30:   required from here
code.hpp:13:25: error: template argument required for ‘class stack_array’
     static const size_t max_sz = 10;
                         ^~~~~~
code.hpp: In instantiation of ‘stack_array<T, C, K>::stack_array(T&) [with T = std::basic_string<char>; C = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >; K = stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >]’:
main.cpp:11:30:   required from here
code.hpp:68:41: error: use of deleted function ‘std::array<stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, 10>::array()’
 stack_array<T,C,K>::stack_array(T& _data){
                                         ^
In file included from code.hpp:3:0:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: note: ‘std::array<stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, 10>::array()’ is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: error: ‘stack<T, C>::stack() [with T = std::basic_string<char>; C = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >]’ is private within this context
code.hpp:42:34: note: declared private here
 template<typename T, typename C> stack<T,C>::stack(){
                                  ^~~~~~~~~~
In file included from code.hpp:3:0:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: error: ‘stack<T, C>::~stack() [with T = std::basic_string<char>; C = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >]’ is private within this context
     struct array
            ^~~~~
code.hpp:45:34: note: declared private here
 template<typename T, typename C> stack<T,C>::~stack(){
                                  ^~~~~~~~~~
code.hpp:68:41: error: use of deleted function ‘std::array<stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, 10>::~array()’
 stack_array<T,C,K>::stack_array(T& _data){
                                         ^
In file included from code.hpp:3:0:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: note: ‘std::array<stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, 10>::~array()’ is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: error: ‘stack<T, C>::~stack() [with T = std::basic_string<char>; C = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >]’ is private within this context
code.hpp:45:34: note: declared private here
 template<typename T, typename C> stack<T,C>::~stack(){
                                  ^~~~~~~~~~
code.hpp:70:5: error: ‘void stack<T, C>::push(T&) [with T = std::basic_string<char>; C = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >]’ is private within this context
     store[curr_idx].push(_data);
     ^~~~~
code.hpp:48:39: note: declared private here
 template<typename T, typename C> void stack<T,C>::push(T& _data){
                                       ^~~~~~~~~~
code.hpp: In instantiation of ‘stack_array<T, C, K>::~stack_array() [with T = std::basic_string<char>; C = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >; K = stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >]’:
main.cpp:11:30:   required from here
code.hpp:76:1: error: use of deleted function ‘std::array<stack<std::basic_string<char>, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, 10>::~array()’
 }
 ^

我不确定模板类的设计是否存在根本性的错误,还是与实例化时所做的错误有关。

欣赏您的想法。

1 个答案:

答案 0 :(得分:0)

您正在使用未声明的size_t。您应该说using std::size_t,或者将其限定为std::size_t,或者包括标头以声明不合格的名称<stddef.h>

您还试图通过将stack_array声明为一个类来使其成为朋友,但这是一个类模板,而不仅仅是一个类:

friend class stack_array;

您需要将其声明为模板:

template<typename T, typename C, typename K>
    friend class stack_array;