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?