471,073 Members | 1,459 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

undefined refference problem

1
Hi Guys

I'm reading Andrew Koenig and Barbara E. Moo 's book, Accelerated C++.
It's the best works for newbie I ever read. The way the authors introduce the language is very interesting. In chaper 11th, they teach us how to implement our own vector. I rewrite the program, but I found a very curious question. When you place the Vec<T>::Vec( ) constructor in the .CPP file, the compliler complain that undefined reference to `Vec<int>::~Vec()'. When you cut and past the function definition in .H file, It's OK.

I past the three files as follow( one is test file):

Veh.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef VEC_H
  2. #define VEC_H
  3.  
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <memory>
  7.  
  8. using std::max;
  9.  
  10. template <class T> class Vec {
  11. public:
  12.     typedef T* iterator;
  13.     typedef const T* const_iterator;
  14.     typedef size_t size_type;
  15.     typedef T value_type;
  16.     typedef T& reference;
  17.     typedef const T& const_reference;
  18.  
  19.     Vec(); 
  20.     explicit Vec(size_type n, const T& t = T());
  21.  
  22.     Vec(const Vec& v);
  23.     Vec& operator=(const Vec&);    // as defined in 11.3.2/196
  24.     ~Vec();// { uncreate(); }
  25.  
  26.     T& operator[](size_type i) { return data[i]; }
  27.     const T& operator[](size_type i) const { return data[i]; }
  28.  
  29.     void push_back(const T& t) {
  30.         if (avail == limit)
  31.             grow();
  32.         unchecked_append(t);
  33.     }
  34.  
  35.     size_type size() const { return avail - data; }  // changed
  36.  
  37.     iterator begin() { return data; }
  38.     const_iterator begin() const { return data; }
  39.  
  40.     iterator end() { return avail; }                 // changed
  41.     const_iterator end() const { return avail; }     // changed
  42.     void clear();// { uncreate(); }
  43.     bool empty() const { return data == avail; }
  44.  
  45. private:
  46.     iterator data;    // first element in the `Vec'
  47.     iterator avail;    // (one past) the last element in the `Vec'
  48.     iterator limit;    // (one past) the allocated memory
  49.  
  50.     // facilities for memory allocation
  51.     std::allocator<T> alloc;    // object to handle memory allocation
  52.  
  53.     // allocate and initialize the underlying array
  54.     void create();
  55.     void create(size_type, const T&);
  56.     void create(const_iterator, const_iterator);
  57.  
  58.     // destroy the elements in the array and free the memory
  59.     void uncreate();
  60.  
  61.     // support functions for `push_back'
  62.     void grow();
  63.     void unchecked_append(const T&);
  64. };
  65. #endif
  66.  

Vec.cpp
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "Vec.h"
  3.  
  4. using namespace std;
  5. template <class T> void Vec<T>::create()
  6. {
  7.     data = avail = limit = 0;
  8. }
  9.  
  10. template <class T> void Vec<T>::create(size_type n, const T& val)
  11. {
  12.     data = alloc.allocate(n);
  13.     limit = avail = data + n;
  14.     std::uninitialized_fill(data, limit, val);
  15. }
  16.  
  17. template <class T>
  18. void Vec<T>::create(const_iterator i, const_iterator j)
  19. {
  20.     data = alloc.allocate(j - i);
  21.     limit = avail = std::uninitialized_copy(i, j, data);
  22. }
  23.  
  24. template <class T> void Vec<T>::uncreate()
  25. {
  26.     if (data) {
  27.         // destroy (in reverse order) the elements that were constructed
  28.         iterator it = avail;
  29.         while (it != data)
  30.             alloc.destroy(--it);
  31.  
  32.         // return all the space that was allocated
  33.         alloc.deallocate(data, limit - data);
  34.     }
  35.     // reset pointers to indicate that the `Vec' is empty again
  36.     data = limit = avail = 0;
  37.  
  38. }
  39.  
  40. template <class T> void Vec<T>::grow()
  41. {
  42.     // when growing, allocate twice as much space as currently in use
  43.     size_type new_size = max(2 * (limit - data), ptrdiff_t(1));
  44.  
  45.     // allocate new space and copy existing elements to the new space
  46.     iterator new_data = alloc.allocate(new_size);
  47.     iterator new_avail = std::uninitialized_copy(data, avail, new_data);
  48.  
  49.     // return the old space
  50.     uncreate();
  51.  
  52.     // reset pointers to point to the newly allocated space
  53.     data = new_data;
  54.     avail = new_avail;
  55.     limit = data + new_size;
  56. }
  57.  
  58. // assumes `avail' points at allocated, but uninitialized space
  59. template <class T> void Vec<T>::unchecked_append(const T& val)
  60. {
  61.     alloc.construct(avail++, val);
  62. }
  63.  
  64. template <class T>
  65. Vec<T>& Vec<T>::operator=(const Vec& rhs)
  66. {
  67.     // check for self-assignment
  68.     if (&rhs != this) {
  69.  
  70.         // free the array in the left-hand side
  71.         uncreate();
  72.  
  73.         // copy elements from the right-hand to the left-hand side
  74.         create(rhs.begin(), rhs.end());
  75.     }
  76.     return *this;
  77. }
  78.  
  79. template<class T>
  80. Vec<T>::Vec()
  81.     create(); 
  82. }
  83.  
  84.  
  85.  
  86. template<class T>
  87. Vec<T>::Vec(Vec::size_type n, const T& t ) 
  88. { create(n, t); }
  89.  
  90.  
  91. template<class T>
  92. Vec<T>::Vec(const Vec& v)
  93. {
  94.     create(v.begin(), v.end());
  95. }
  96.  
  97. template<class T>
  98. void Vec<T>::clear() { uncreate(); }
  99.  
  100. template<class T>
  101. Vec<T>::~Vec()
  102.     uncreate(); 
  103. }
Sep 18 '07 #1
4 1626
Ganon11
3,652 Expert 2GB
Not sure what the problem is: I'm a bad programmer who puts the .cpp content right in with the .h file, and I've never figured out how to separate them T_T.
Sep 19 '07 #2
RRick
463 Expert 256MB
It also depends on the compiler. If you're using GNU g++, then you must put all the code in the header file.
Sep 19 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
All templates must go in a header file. They are not like functions. They are patterns for the compiler to use to generate functions. If the pattern is not present, the generated function cannot be made and you die with a compile error.

C++ provides for export templates but I have yet to see a compiler implement it.
Sep 24 '07 #4
RRick
463 Expert 256MB
I never realized just how many companies, haven't and won't implement "export templates".

I thought GNU was the exception to this feature, but now realize that GNU is in the majority, the vast majority.
Sep 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Matthew Thorley | last post: by
8 posts views Thread by Scott J. McCaughrin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.