0

I've recently tried to write a Tiny STL on my own. However, it seems to fail after finishing implementing the vector container. I've got some errors occurring in the 'xutility' class. I'm afraid it might come from the template code, but I have no idea about how to deal with it. Any suggestions?

The class definition and the member-function declaration are defined in vector.h

// vector.h
#ifndef _VECTOR_H_
#define _VECTOR_H_

#include <initializer_list>
#include <cstddef>
#include <memory>

#include "../Declaration/iterator.h"
#include "../Declaration/type_traits.h"
namespace MySTL{
    template<typename T>
    class vector{
    public:
        typedef std::size_t                     size_type;
        typedef T                               value_type;
        typedef value_type&                     reference;
        typedef const value_type&               const_reference;
        typedef value_type*                     iterator;
        typedef const value_type*               const_iterator;
        typedef value_type*                     pointer;
        typedef const value_type*               const_pointer;
        typedef std::reverse_iterator<T*>       reverse_iterator;
        typedef std::reverse_iterator<const T*> const_reverse_iterator;
        typedef std::ptrdiff_t                  difference_type;
    public:
        vector() : elements(nullptr), first_free(nullptr), cap(nullptr) {}
        vector(const vector &); 
        vector(vector &&); //noexcept;
        // explicit 
        vector(const size_type n);  
        vector(const size_type n, const value_type &val);
        vector(std::initializer_list<value_type> il);
        template<typename InputIterator>
        vector(InputIterator first, InputIterator second);

        vector& operator=(const vector &rhs);
        vector& operator=(std::initializer_list<value_type> il);
        vector& operator=(vector &&rhs); //noexcept;
        ~vector();

        // TOO MANY CODES TO DISPLAY 
        // SO I HAVE TO OMIT SOME OF THEM
        // I'M SURE THEY ARE INNOCENT
        // :-)

    private:
        // ...
        std::allocator<T> alloc;
        T* elements;    // head pointer
        T* first_free;  // the pointer that point to the first free element in the array
        T* cap;         // end of storage
    };
}

#include "../Implementation/vector_impl.h"  // inclusion compilation model

#endif // _VECTOR_H_

while the implementation of member functions are defined in vector_impl.h

// vector_impl.h
#ifndef _VECTOR_IMPL_H_
#define _VECTOR_IMPL_H_

#include <utility>
#include <memory>
#include <algorithm>
#include <stdexcept>

#include "../Declaration/vector.h"

using std::uninitialized_fill_n;
using std::uninitialized_copy;

namespace MySTL
{
    template <typename T>
    vector<T>::vector(const vector &s)
    {
        auto newdata = alloc_n_copy(s.begin(), s.end());
        elements = newdata.first;
        first_free = cap = newdata.second;
    }


    template <typename T>
    vector<T>::vector(vector &&s) //noexcept
        : elements(s.elements), first_free(s.first_free), cap(s.cap)
    {
        s.elements = s.first_free = s.cap = nullptr;
    }

    // I'VE TO OMIT MAJORITY OF FUNCTION IMPLEMENTATIONS BECAUSE THEY ARE PROLIXITY
    // YOU CAN FIND IT THROUGH THE QUICK LINKS MENTIONED BEFORE. :-)

}
#endif

Then I wrote a test case and use the main function to invoke it, and I got the following errors:

error   13  error C2868: 'std::iterator_traits<_InIt>::value_type': illegal syntax for using-declaration; expected qualified-name   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   28  error C2868: 'std::iterator_traits<_InIt>::reference': illegal syntax for using-declaration; expected qualified-name    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   23  error C2868: 'std::iterator_traits<_InIt>::pointer': illegal syntax for using-declaration; expected qualified-name  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   8   error C2868: 'std::iterator_traits<_InIt>::iterator_category': illegal syntax for using-declaration; expected qualified-name    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   18  error C2868: 'std::iterator_traits<_InIt>::difference_type': illegal syntax for using-declaration; expected qualified-name  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   4   error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   9   error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   14  error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   19  error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   24  error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   30  error C2665: 'std::_Uninitialized_copy0': none of the 2 overloads could convert all the argument types  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory  337 1   MySTL
error   29  error C2665: 'std::_Debug_range2': none of the 2 overloads could convert all the argument types d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 584 1   MySTL
error   12  error C2602: 'std::iterator_traits<_InIt>::value_type' is not a member of a base class of 'std::iterator_traits<_InIt>' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   27  error C2602: 'std::iterator_traits<_InIt>::reference' is not a member of a base class of 'std::iterator_traits<_InIt>'  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   22  error C2602: 'std::iterator_traits<_InIt>::pointer' is not a member of a base class of 'std::iterator_traits<_InIt>'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   7   error C2602: 'std::iterator_traits<_InIt>::iterator_category' is not a member of a base class of 'std::iterator_traits<_InIt>'  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   17  error C2602: 'std::iterator_traits<_InIt>::difference_type' is not a member of a base class of 'std::iterator_traits<_InIt>'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   11  error C2146: syntax error : missing ';' before identifier 'value_type'  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   26  error C2146: syntax error : missing ';' before identifier 'reference'   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   21  error C2146: syntax error : missing ';' before identifier 'pointer' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   6   error C2146: syntax error : missing ';' before identifier 'iterator_category'   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   16  error C2146: syntax error : missing ';' before identifier 'difference_type' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   10  error C2039: 'value_type': is not a member of '`global namespace''  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   25  error C2039: 'reference': is not a member of '`global namespace''   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   20  error C2039: 'pointer': is not a member of '`global namespace'' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   5   error C2039: 'iterator_category': is not a member of '`global namespace''   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   15  error C2039: 'difference_type': is not a member of `global namespace''  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL

Is there anyone that can kindly help me?

Pang
  • 9,564
  • 146
  • 81
  • 122
  • the source code can be get from my [github](https://github.com/MarinYoung4596/MySTL) – user3560076 Nov 07 '15 at 06:43
  • Unrelated to your question, but don't use the symbol `_VECTOR_IMPL_H_`, leading underscore followed by an upper-case letter is reserved fothe "implementation" (compiler/standard library). Don't use such names unless you actually try to create a complete standard library. [Reference](http://stackoverflow.com/a/228797/440558). – Some programmer dude Nov 07 '15 at 06:47
  • @JoachimPileborg: I finally arrived to the conclusion that's a lost battle. There is an insane attraction to these names **BEACUSE** they are reserved. – 6502 Nov 07 '15 at 06:52
  • *"I've recently tried to write a Tiny STL on my own"* - I hope this is for academic purposes and not for production code. – Christian Hackl Nov 07 '15 at 11:10
  • @JoachimPileborg Thank you so much, I'll try to figure it out. :-) – user3560076 Nov 08 '15 at 06:08
  • @ChristianHackl Of course not because I used it to practise my programming skills as i am new to C++ programming :-) – user3560076 Nov 08 '15 at 06:12

1 Answers1

0

I believe you get into trouble here:

using std::uninitialized_fill_n;
using std::uninitialized_copy;

because these algorithms (on the lines around xmemory 337 in the error messages) contain code to validate the iterators used in the call ("checked iterators"). Your iterator being a pointer doesn't fit into the code of the original library.

You might want to implement these functions yourself as a workaround (but also notice that a real vector implementation would have to use the allocator to construct in uninitialized memory).

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Appreciate that....So what should I do next? Use type cast in front of the function call? Or implement these functions by myself? Please give me more hints for I'm a rookie in C++ programming. Thank you so much :-) – user3560076 Nov 08 '15 at 06:31