派生 class 中的函数无法访问基 class 中的成员

Functions in derived class not being able to access members in base class

#include <memory>
template<typename T> struct vector_base{
    std::allocator<T> alloc;
    T *elem;
    int sz;
    int space;
    vector_base(const std::allocator<T> &a, int n):alloc{a},elem{alloc.allocate(n)},sz{n},space{n}{}
    ~vector_base(){alloc.deallocate(elem,space);}
};
template<typename T> class vector:private vector_base<T> {
public:
    vector():sz{0},elem{nullptr},space{0}{}
    explicit vector(int s):sz{s},elem{new T[s]}, space{s}{
        for(int i = 0; i<sz; i++) elem[i]=0;
    }
    T &at(int n);
    const T &at(int n) const;
    void reserve(int newalloc);
    void resize(int newsize, T def=T());
    int capacity() const { return space;}
    void push_back(int d);
    vector &operator=(const vector &a);
};

编辑:这是编译日志

prog.cpp: In constructor ‘vector<T>::vector()’:
prog.cpp:12:14: error: class ‘vector<T>’ does not have any field named ‘sz’
     vector():sz{0},elem{nullptr},space{0}{}
              ^~
prog.cpp:12:20: error: class ‘vector<T>’ does not have any field named ‘elem’
     vector():sz{0},elem{nullptr},space{0}{}
                    ^~~~
prog.cpp:12:34: error: class ‘vector<T>’ does not have any field named ‘space’
     vector():sz{0},elem{nullptr},space{0}{}
                                  ^~~~~
prog.cpp: In constructor ‘vector<T>::vector(int)’:
prog.cpp:13:28: error: class ‘vector<T>’ does not have any field named ‘sz’
     explicit vector(int s):sz{s},elem{new T[s]}, space{s}{
                            ^~
prog.cpp:13:34: error: class ‘vector<T>’ does not have any field named ‘elem’
     explicit vector(int s):sz{s},elem{new T[s]}, space{s}{
                                  ^~~~
prog.cpp:13:50: error: class ‘vector<T>’ does not have any field named ‘space’
     explicit vector(int s):sz{s},elem{new T[s]}, space{s}{
                                                  ^~~~~
prog.cpp:14:26: error: ‘sz’ was not declared in this scope
         for(int i = 0; i<sz; i++) elem[i]=0;
                          ^~
prog.cpp:14:26: note: suggested alternative: ‘s’
         for(int i = 0; i<sz; i++) elem[i]=0;
                          ^~
                          s
prog.cpp:14:35: error: ‘elem’ was not declared in this scope
         for(int i = 0; i<sz; i++) elem[i]=0;
                                   ^~~~
prog.cpp:14:35: note: suggested alternative: ‘enum’
         for(int i = 0; i<sz; i++) elem[i]=0;
                                   ^~~~
                                   enum
prog.cpp: In member function ‘int vector<T>::capacity() const’:
prog.cpp:20:35: error: ‘space’ was not declared in this scope
     int capacity() const { return space;}
                                   ^~~~~

经过一番摸索,我认为我的问题是我需要以某种方式调用基本构造函数并找到一种方法让 class vector 中的函数访问 [=15] 中的成员=].

我应该怎么做,我完全不知道。

您不能直接初始化基 class 的成员,这会与任何意义上的 class 封装相矛盾。这就是为什么你在基础 class 中编写构造函数的原因,你可以像这样调用它们:

template<typename T> class vector:private vector_base<T> {
public:
    std::allocator<T> myalloc;
    vector() : vector_base<T>(myalloc, 0) {
    }

当然我想知道你为什么只有那种构造函数。你的问题很复杂,私有继承几乎不是你想要的。另外你的问题似乎不完整。