Файл .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()’
}
^
Я не уверен, есть ли что-то принципиально неправильное в дизайне шаблонных классов, или это связано с чем-то неправильным, сделанным во время создания экземпляра,
Цените ваши мысли.