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 1781
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.
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.
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,...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |