Recoder la classe Vector<T>
Bonjour,
En essayant recorder une version simplifiée de la classe vector<T> (en suivant le livre Advanced C++ programming), appelée Vec<T>. Mon programme se plante dès l'appel du constructeur de la classe
Code:
1 2 3 4 5 6 7 8
| #include "stdafx.h"
#include <iostream>
#include "Vec.h"
int main()
{
Vec<int> x;
} |
Les constructeurs et le destructeur de la classe appellent respectivement les fonctions create() et uncreate() qui permettent une gestion de mémoire flexible lors de l'appel de push_back() via l'utilisation de la classe allocator<T> .
Je ne comprend pas l'erreur affiché par le compilateur :
Code:
1 2 3 4 5
| LNK2019 symbole externe non résolu "private: void __thiscall Vec<int>::create(void)" (?create@?$Vec@H@@AAEXXZ) référencé dans la fonction "public: __thiscall Vec<int>::Vec<int>(void)" (??0?$Vec@H@@QAE@XZ)
LNK2019 symbole externe non résolu "private: void __thiscall Vec<int>::uncreate(void)" (?uncreate@?$Vec@H@@AAEXXZ) référencé dans la fonction "public: __thiscall Vec<int>::~Vec<int>(void)" (??1?$Vec@H@@QAE@XZ)
LNK1120 2 externes non résolus |
Quelqu'un peut m'aider svp? Merci
--------------------------------------------------- Vec.h -------------------------------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <memory>
template <class T> class Vec
{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;
Vec()
{
create();
}
explicit Vec(size_type n, const T& t = T())
{
create(n, t);
}
// The rule of three
Vec(const Vec& v)
{
create(v.begin(), v.end());
}
Vec& operator=(const Vec&);
~Vec()
{
uncreate();
}
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;
// allocate and initialize the underlying array
void create();
void create(const_iterator, const_iterator);
// destroy the elements in the array and free the memory
void uncreate();
}; |
--------------------------------------------------- Vec.cpp -------------------------------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include "Vec.h"
#include <memory>
using std::allocator;
using std::uninitialized_fill;
using std::uninitialized_copy;
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> 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;
uninitialized_fill(data, limit, val); // reproduire 3 dans le vecteur de 1 à 2 // 1,2 <- 3
}
template<class T> void Vec<T>::create(const_iterator i, const_iterator j)
{
data = alloc.allocate(j - i);
limit = avail = uninitialized_copy(i, j, data); // copier le vecteur de 1 à 2 dans 3 // 1,2 -> 3
}
template<class T> void Vec<T>::uncreate()
{
if (data) // alloc.deallocate() requires nonzero pointer à l'inverse de delete
{
// 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 = avail = limit = 0;
} |