-2

The .hpp file below compiles without any errors or warnings:

#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

But if the only two lines in main() are uncommented:

#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;
    }

all hell breaks loose, and I get a bunch of errors:

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()’
 }
 ^

I am not sure whether there is something fundamentally wrong with the design of the template classes, or it has to do with something wrong done at the time of instantiation.

Appreciate your thoughts.

Vinod
  • 925
  • 8
  • 9

1 Answers1

0

You are using size_t which is not declared. You should either say using std::size_t, or qualify it as std::size_t, or include the header to declare the unqualified name, <stddef.h>.

You're also trying to make stack_array a friend by declaring it as a class, but it's a class template not just a class:

friend class stack_array;

You need to declare it as a template:

template<typename T, typename C, typename K>
    friend class stack_array;
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • I had already tried the second suggestion (the corresponding line of code had been commented).....the first suggestion doesn't help either – Vinod Jul 08 '19 at 10:16