简体   繁体   中英

How do you use vector as a base class

Win7 Cygwin

This the first time I've used templates & containers. I don't understand the errors. To my (naive) way of looking at things, I have defined an allocator (_Alloc) and a typdef (allocator_Type). The messages appear to be saying that I haven't done my homework properly. I have no clue as to what to do.

The code is:

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) { };
};

The error messsages are:

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) { };
                                                                  ^

This has been stated in comments but not posted as an answer yet: you don't .

The standard library containers are not supposed to be used as (public) base classes. They are non-polymorphic, so you can't pass an object derived from one safely to any function which expects a pointer or reference to the container.

You could , in theory, use a standard container as private base class. This has the same semantics as a private member variable, but it would make your code a lot easier to follow for others if you just used a private member variable.

First - you have a typo: uppercase T in allocator_Type and lowercase in const allocator_Type& alloc = allocator_type(). Second - std::vector destructor is not virtual, hence you really should not derive from it unless you never cast and delete via base class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM