【发布时间】:2016-02-08 09:03:29
【问题描述】:
我最近尝试自己编写一个 Tiny STL。但是,在完成向量容器的实现后,它似乎失败了。我在“xutility”类中发生了一些错误。恐怕它可能来自模板代码,但我不知道如何处理它。有什么建议吗?
类定义和成员函数声明在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_
而成员函数的实现在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
然后我写了一个测试用例,用main函数调用,报错如下:
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
有没有人可以帮助我?
【问题讨论】:
标签: c++ templates c++11 visual-c++ vector