C++ 如何使用vector作为基类

C++ 如何使用vector作为基类,c++,templates,inheritance,c++11,C++,Templates,Inheritance,C++11,Win7 西格温 这是我第一次使用模板和容器。我不理解这些错误。按照我(天真的)看待事物的方式,我定义了一个分配器(_Alloc)和一个typdef(分配器类型)。这些信息似乎在说我的家庭作业做得不好。我不知道该怎么办 代码是: template<typename T, typename _Alloc = std::allocator<T>> class Array : public vector<T, _Alloc> { public: t

Win7 西格温

这是我第一次使用模板和容器。我不理解这些错误。按照我(天真的)看待事物的方式,我定义了一个分配器(_Alloc)和一个typdef(分配器类型)。这些信息似乎在说我的家庭作业做得不好。我不知道该怎么办

代码是:

template<typename T, typename _Alloc = std::allocator<T>>
class Array : public vector<T, _Alloc> {
   public:
      typedef T         value_type;
      typedef _Alloc    allocator_Type;
   private:
   int compar (const void* p1, const void* p2) { T v1 = (T)*p1;
                                                 T v2 = (T)*p2;
                                                 return (v1 < v2)? -1: (v1 > v2)? 1: 0;   }
   public:
   explicit Array (const allocator_Type& alloc = allocator_type()) : vector<T, _Alloc>(alloc)    { };
   explicit Array (size_t n)                                       : vector<T, _Alloc>(n)        { };
            Array (size_t n, const T& val,
                    const allocator_type& alloc = allocator_type()): vector<T, _Alloc>(n, val, alloc) { };
};
模板
类数组:公共向量{
公众:
类型定义T值_类型;
typedef_Alloc分配器_Type;
私人:
int compar(const void*p1,const void*p2){T v1=(T)*p1;
T v2=(T)*p2;
返回值(v1v2)?1:0;}
公众:
显式数组(const allocator_Type&alloc=allocator_Type()):vector(alloc){};
显式数组(size_t n):向量(n){};
数组(大小、常数和值、,
常量分配器类型&alloc=allocator类型()):向量(n,val,alloc){};
};
错误消息包括:

main.cpp:31:27: error: 'allocator_type' does not name a type
                     const allocator_type& alloc = allocator_type()): vector<T, _Alloc>(n, val, alloc) { };
                           ^
main.cpp:31:27: note: (perhaps 'typename std::vector<_Tp, _Alloc>::allocator_type' was intended)
main.cpp:31:66: warning: ISO C++ forbids declaration of 'alloc' with no type [-fpermissive]
                     const allocator_type& alloc = allocator_type()): vector<T, _Alloc>(n, val, alloc) { };
                                                                  ^
main.cpp:28:65: warning: there are no arguments to 'allocator_type' that depend on a template parameter, so a declaration of 'allocator_type' must be available [-fpermissive]
    explicit Array (const allocator_Type& alloc = allocator_type()) : vector<T, _Alloc>(alloc)    { };
                                                                 ^
main.cpp:31:66: warning: there are no arguments to 'allocator_type' that depend on a template parameter, so a declaration of 'allocator_type' must be available [-fpermissive]
                     const allocator_type& alloc = allocator_type()): vector<T, _Alloc>(n, val, alloc) { };
                                                                  ^
main.cpp:31:27:错误:“分配器类型”未命名类型
常量分配器类型&alloc=allocator类型()):向量(n,val,alloc){};
^
main.cpp:31:27:注:(可能是有意使用“typename std::vector::allocator_type”)
CPP:31:66:警告:ISC++禁止声明“没有”类型的“同种”。
常量分配器类型&alloc=allocator类型()):向量(n,val,alloc){};
^
main.cpp:28:65:警告:“分配器类型”没有依赖于模板参数的参数,因此“分配器类型”的声明必须可用[-fppermissive]
显式数组(const allocator_Type&alloc=allocator_Type()):vector(alloc){};
^
main.cpp:31:66:警告:“分配器类型”没有依赖于模板参数的参数,因此“分配器类型”的声明必须可用[-fppermissive]
常量分配器类型&alloc=allocator类型()):向量(n,val,alloc){};
^

首先-您有一个输入错误:分配器类型中的大写T和常量分配器类型中的小写&alloc=allocator\u Type()。第二个-std::vector析构函数不是虚拟的,因此除非您从未通过基类强制转换和删除,否则您不应该从它派生。

这已在评论中说明,但尚未作为答案发布:您没有

标准库容器不应用作(公共)基类。它们是非多态的,因此您无法将从一个派生的对象安全地传递给任何需要指向容器的指针或引用的函数


理论上,您可以使用标准容器作为私有基类。这与私有成员变量具有相同的语义,但如果只使用私有成员变量,则会使其他人更容易理解您的代码。

注意,
std::vector
的析构函数未声明为虚拟,因此不应用作基类。“如何将vector用作基类”-您不这样做
std::vector
被设计为封装的,而不是继承的。
typedef\u Alloc分配器\u Type
是实际名称。你用小写的t写了
allocator\u type
。@ArthurSchwarez不要被教条主义者吓到。如果从容器派生,则不会发生任何坏事。只是不要通过指向基地的指针删除…@ArthurSchwarez他们说不应该…不可能。请参阅以获取各种意见。曾经想过将“很酷的特性”添加到vector(比如将其限制在最大大小)的人发现,必须非常小心地确保覆盖所有修改例程以添加检查。如果你依赖它,那么突然间你就不能切换到另一个系列了。与在类设计中使用与域匹配的标准容器相比,粒度通常是错误的。