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 -
#ifndef VEC_H
-
#define VEC_H
-
-
#include <algorithm>
-
#include <cstddef>
-
#include <memory>
-
-
using std::max;
-
-
template <class T> class Vec {
-
public:
-
typedef T* iterator;
-
typedef const T* const_iterator;
-
typedef size_t size_type;
-
typedef T value_type;
-
typedef T& reference;
-
typedef const T& const_reference;
-
-
Vec();
-
explicit Vec(size_type n, const T& t = T());
-
-
Vec(const Vec& v);
-
Vec& operator=(const Vec&); // as defined in 11.3.2/196
-
~Vec();// { uncreate(); }
-
-
T& operator[](size_type i) { return data[i]; }
-
const T& operator[](size_type i) const { return data[i]; }
-
-
void push_back(const T& t) {
-
if (avail == limit)
-
grow();
-
unchecked_append(t);
-
}
-
-
size_type size() const { return avail - data; } // changed
-
-
iterator begin() { return data; }
-
const_iterator begin() const { return data; }
-
-
iterator end() { return avail; } // changed
-
const_iterator end() const { return avail; } // changed
-
void clear();// { uncreate(); }
-
bool empty() const { return data == avail; }
-
-
private:
-
iterator data; // first element in the `Vec'
-
iterator avail; // (one past) the last element in the `Vec'
-
iterator limit; // (one past) the allocated memory
-
-
// facilities for memory allocation
-
std::allocator<T> alloc; // object to handle memory allocation
-
-
// allocate and initialize the underlying array
-
void create();
-
void create(size_type, const T&);
-
void create(const_iterator, const_iterator);
-
-
// destroy the elements in the array and free the memory
-
void uncreate();
-
-
// support functions for `push_back'
-
void grow();
-
void unchecked_append(const T&);
-
};
-
#endif
-
Vec.cpp -
-
#include "Vec.h"
-
-
using namespace std;
-
template <class T> void Vec<T>::create()
-
{
-
data = avail = limit = 0;
-
}
-
-
template <class T> void Vec<T>::create(size_type n, const T& val)
-
{
-
data = alloc.allocate(n);
-
limit = avail = data + n;
-
std::uninitialized_fill(data, limit, val);
-
}
-
-
template <class T>
-
void Vec<T>::create(const_iterator i, const_iterator j)
-
{
-
data = alloc.allocate(j - i);
-
limit = avail = std::uninitialized_copy(i, j, data);
-
}
-
-
template <class T> void Vec<T>::uncreate()
-
{
-
if (data) {
-
// destroy (in reverse order) the elements that were constructed
-
iterator it = avail;
-
while (it != data)
-
alloc.destroy(--it);
-
-
// return all the space that was allocated
-
alloc.deallocate(data, limit - data);
-
}
-
// reset pointers to indicate that the `Vec' is empty again
-
data = limit = avail = 0;
-
-
}
-
-
template <class T> void Vec<T>::grow()
-
{
-
// when growing, allocate twice as much space as currently in use
-
size_type new_size = max(2 * (limit - data), ptrdiff_t(1));
-
-
// allocate new space and copy existing elements to the new space
-
iterator new_data = alloc.allocate(new_size);
-
iterator new_avail = std::uninitialized_copy(data, avail, new_data);
-
-
// return the old space
-
uncreate();
-
-
// reset pointers to point to the newly allocated space
-
data = new_data;
-
avail = new_avail;
-
limit = data + new_size;
-
}
-
-
// assumes `avail' points at allocated, but uninitialized space
-
template <class T> void Vec<T>::unchecked_append(const T& val)
-
{
-
alloc.construct(avail++, val);
-
}
-
-
template <class T>
-
Vec<T>& Vec<T>::operator=(const Vec& rhs)
-
{
-
// check for self-assignment
-
if (&rhs != this) {
-
-
// free the array in the left-hand side
-
uncreate();
-
-
// copy elements from the right-hand to the left-hand side
-
create(rhs.begin(), rhs.end());
-
}
-
return *this;
-
}
-
-
template<class T>
-
Vec<T>::Vec()
-
{
-
create();
-
}
-
-
-
-
template<class T>
-
Vec<T>::Vec(Vec::size_type n, const T& t )
-
{ create(n, t); }
-
-
-
template<class T>
-
Vec<T>::Vec(const Vec& v)
-
{
-
create(v.begin(), v.end());
-
}
-
-
template<class T>
-
void Vec<T>::clear() { uncreate(); }
-
-
template<class T>
-
Vec<T>::~Vec()
-
{
-
uncreate();
-
}
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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()
|
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.
|
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?
=================
|
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...
|
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
| |
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...
|
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()
|
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
:-)
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |