473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

undefined refference problem

1 New Member
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 1816
Ganon11
3,652 Recognized Expert Specialist
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 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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
1237
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, even unecessarily complicated mumbo-jumbo. Let me show you a snippet and then I'll explain what's happening. for verse in self.activeSong: verseNum = self.activeSong.index(verse) activeSong = self.activeSong.copy()
2
13124
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 Unixware/C++/AT&T componets environment. I have been able to compile all modules after making necessary changes in LINUX/gcc/STL environment. We have two templates defined XList and XMap.
8
4589
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
1
31186
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 joy. I got dev-c++ and a bit of winsock sample code. I've done nothing out of the ordinary. I could only assume that anyone else that downloaded this software and attempted this would meet with the same result. The problem lies with either the...
1
3994
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 proccess request ---> object refference not set to an instance of an object Following is the web method: Public Structure vbVINVariables Public mModel As String Public mYear As String
2
2507
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 the OID of Person), the information of the person matching the selected OID is shown in the Textboxes (Name, Address, id, etc) and the courses this person is taken are shown in a DataGrid (course name, price, etc.). This is working well so far, I...
7
1556
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 http://www.ualberta.ca/~koryb/ http://www.ualberta.ca/~koryb/test.html runs it // Runs timer loop and increments pics function timer()
45
4855
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 "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.