C++ Primer第18章Vector的再实现及bug修正

C++Primer第18.1.2节在介绍allocator类的时候,给了一个仿照标准库中vector的例子。感觉示例代码非常好,但是本人发现了一个bug,与大家共享。

按照作者的示例程序,编译程序时总是在alloc.construct()函数处报错,不同IDE可能提示的错误原因不同,本人的是undefined reference to `Vector<std::string>::alloc'。

仔细想想,应该与类Vector中alloc成员的静态属性有关,因此有两种修正方式:

 1)删去static关键字

 2)在全局作用域添加:template<class T> std::allocator<T> Vector<T>::alloc;

测试源代码如下:

 1 #include <iostream>
 2 #include <string>
 3 #include <memory>
 4 using namespace std;
 5 
 6 std::ptrdiff_t max(std::ptrdiff_t a, std::ptrdiff_t b)
 7 {
 8     return a > b ? a : b;
 9 }
10 
11 template <class T> class Vector{
12 public:
13     Vector(): elements(0), first_free(0), end(0) {}
14     void push_back(const T&);
15 
16 private:
17     static std::allocator<T> alloc;
18     void reallocate();
19 public:
20     T * elements;
21     T * first_free;
22     T * end;
23 };
24 
25 template<class T> std::allocator<T> Vector<T>::alloc;//如果没有这句,编译器报错,但如果类Vector中去掉static关键字,这句可以删掉
26 
27 template <class T>
28 void Vector<T>::push_back(const T& t)
29 {
30     if(first_free == end)
31         reallocate();
32     alloc.construct(first_free,t);
33     ++first_free;
34 }
35 
36 template <class T>
37 void Vector<T>::reallocate()
38 {
39     std::ptrdiff_t size = first_free - elements;
40     std::ptrdiff_t newcapacity = 2 * max(size, 1);
41 
42     T * newelements = alloc.allocate(newcapacity);
43 
44     uninitialized_copy(elements, first_free, newelements);
45 
46     for(T * p = first_free; p != elements; alloc.destroy(--p));
47 
48     if(elements)
49         alloc.deallocate(elements, end - elements);
50 
51     elements = newelements;
52     first_free = elements + size;
53     end = elements + newcapacity;
54 }
55 
56 int main()
57 {
58     Vector<string> str;
59     string s;
60     for(int i = 0; i < 5; ++i)
61     {
62         cin>>s;
63         str.push_back(s);
64     }
65     for(string * p = str.elements; p != str.first_free; ++p)
66         cout<<*p<<endl;
67     return 0;
68 }

虽然知道了解决办法,但是对于bug的原因并不是很清楚,欢迎大家留言交流及指教。

posted @ 2015-10-07 17:01  木子可  阅读(349)  评论(0编辑  收藏  举报