473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1781
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

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

Similar topics

4
by: Matthew Thorley | last post by:
Greetings, Maybe someone out there can lend me an eye? I've been stumped, banging my head against the wall trying to figure out why my script doesn't work. I've tried every thing I could think of,...
2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
1
by: Dom | last post by:
I'm new to c++. Just started learning it 24 hours ago. Am running into a compile problem. Please, no one waste the effort telling me to google it. I've been researching it for quite a while with no...
1
by: shade1383 | last post by:
I have a web service that runs fine on my local machine and works fine with a VB .Net project remotely, but when I try to run using Access 2002 I get the following error. Server was unable to...
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
7
by: Lae. | last post by:
I can't figure this one out. n00b question no doubt, this is my first ever JS attempt. Here's the snippet, and here's the full deal http://www.ualberta.ca/~koryb/first.js...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.