我正在尝试实现我自己的vector类作为Accelerated C ++一书的练习,但是我遇到了这些错误,我无法弄清楚如何修复它们:
C:\ MinGW \ bin .. \ lib \ gcc \ mingw32 \ 3.4.2 ........ \ include \ c ++ \ 3.4.2 \ bits \ allocator.h ||对vec的未定义引用::创建()'|
::〜VEC()] +×43):C:\ MinGW的\ BIN .. \ lib中\ GCC \的mingw32 \ 3.4.2 ........ \包括\ C ++ \ 3.4.2 \位\ allocator.h ||对`vec :: uncreate()'|
的未定义引用我正在使用code :: blocks,两个文件都在同一个项目中,所以我很确定这不是一个链接问题。
这是vec.h文件
#ifndef VEC_H
#define VEC_H
#include <cstddef>
#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& val = T()){
create(n,val);
}
vec(const vec& v){
create(v.begin(), v.end());
}
~vec(){
uncreate();
}
size_type size() const{
return avail - data;
}
T& operator[](size_type i){
return data[i];
}
const T& operator[](size_type i) const{
return data[i];
}
vec& operator = (const vec&);
void push_back(const T& t){
if(avail == limit)
grow();
unchecked_append(t);
}
iterator begin(){ return data; }
const_iterator begin() const{ return data; }
iterator end(){ return avail; }
const_iterator end() const{ return avail; }
private:
iterator data; //first element
iterator avail;//one past the last available element
iterator limit;//one past the total 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_H
和vec.cpp文件
#include "vec.h"
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);
}
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);
}
template <class T> void vec<T>::uncreate(){
if(data){
//destroy in reverse order the objects that were constructed
iterator it = avail;
while(it != data)
destroy(--it);
//return all the space that was allocated
alloc.deallocate(data, limit-data);
}
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 = 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 to allocated, but uninitialized space
template <class T> void vec<T>::unchecked_append(const T& val){
alloc.construct(avail++, val);
}
我做错了什么?
答案 0 :(得分:1)
由于您的类是模板,您需要将实现放入头文件中,而不是放在.cpp文件中,因为编译期间编译器需要完整的类定义。
答案 1 :(得分:0)
必须在使用它们的每个翻译单元中定义模板;通过将函数定义放入源文件中,它们只在一个单元中定义,如果您尝试在另一个单元中使用它们,则会出错。
在类定义之后将函数定义移动到标题中。