尝试实现我自己的vector类,不断得到;“未定义引用”;错误 我试图把我自己的向量类作为加速C++的一个练习来实现,但是我得到了这些错误,我不知道如何修复它们:

尝试实现我自己的vector类,不断得到;“未定义引用”;错误 我试图把我自己的向量类作为加速C++的一个练习来实现,但是我得到了这些错误,我不知道如何修复它们:,c++,C++,C:\MinGW\bin..\lib\gcc\mingw32\3.4.2……。\include\C++\3.4.2\bits\allocator.h | |未定义对“vec::create()”的引用| ::~vec()]+0x43):C:\MinGW\bin..\lib\gcc\mingw32\3.4.2……。\include\C++\3.4.2\bits\allocator.h | |对“vec::uncreate()”的未定义引用| 我使用的是code::blocks,两个文件都在同一个项

C:\MinGW\bin..\lib\gcc\mingw32\3.4.2……。\include\C++\3.4.2\bits\allocator.h | |未定义对“vec::create()”的引用|

::~vec()]+0x43):C:\MinGW\bin..\lib\gcc\mingw32\3.4.2……。\include\C++\3.4.2\bits\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
\ifndef VEC\H
#定义向量
#包括
#包括
模板类vec{
公众:
typedef T*迭代器;
typedef常量T*常量迭代器;
typedef size_t size_type;
类型定义T值_类型;
vec(){
创建();
}
显式向量(大小\类型n,常数T&val=T()){
创建(n,val);
}
矢量控制(常数矢量控制和矢量控制){
创建(v.begin(),v.end());
}
~vec(){
取消创建();
}
大小\类型大小()常量{
返回有效数据;
}
T&operator[](尺寸\类型i){
返回数据[i];
}
常数T和运算符[](尺寸类型i)常数{
返回数据[i];
}
向量和运算符=(常量向量和);
无效推回(施工T&T){
如果(可用==限制)
增长();
未检查的_追加(t);
}
迭代器begin(){返回数据;}
常量迭代器begin()常量{返回数据;}
迭代器end(){return avail;}
常量迭代器end()常量{return avail;}
私人:
迭代器数据;//第一个元素
迭代器avail;//超过最后一个可用元素
迭代器限制;//超过总分配内存的一个
//内存分配设施
std::allocator alloc;//处理内存分配的对象
//分配并初始化基础数组
void create();
创建空心(尺寸类型、常数和);
void create(常量迭代器,常量迭代器);
//销毁阵列中的元素并释放内存
void uncreate();
//支持推回功能
空洞生长();
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);
}
#包括“vec.h”
模板向量和向量::运算符=(常量向量和rhs){
//检查自我分配
如果(&rhs!=此){
//释放左侧的阵列
取消创建();
//将图元从右侧复制到左侧
创建(rhs.begin(),rhs.end());
}
归还*这个;
}
模板void vec::create(){
数据=avail=limit=0;
}
模板void vec::create(大小\类型n,常数T&val){
数据=分配分配(n);
极限=有效=数据+n;
未初始化的填充(数据、限制、val);
}
模板void vec::create(常量迭代器i,常量迭代器j){
数据=分配分配(j-i);
限制=可用=未初始化的拷贝(i,j,数据);
}
模板void vec::uncreate(){
如果(数据){
//按相反的顺序销毁构建的对象
迭代器it=avail;
while(it!=数据)
摧毁(--它);
//返回已分配的所有空间
分配解除分配(数据、限额数据);
}
数据=限值=有效值=0;
}
模板void vec::grow(){
//增长时,分配两倍于当前使用的空间
尺寸类型新尺寸=最大值(2*(极限数据),ptrdiff_t(1));
//分配新空间并将现有图元复制到新空间
迭代器new_data=alloc.allocate(新_大小);
迭代器new_avail=未初始化的_副本(数据、可用、新_数据);
//返回旧空间
取消创建();
//重置指针以指向新分配的空间
数据=新数据;
avail=新的_avail;
限制=数据+新尺寸;
}
//假定可用点为已分配但未初始化的空间
模板无效向量::未选中的附加(常量T&val){
alloc.construct(avail++,val);
}

我做错了什么?

由于类是一个模板,您需要将实现放在头文件中,而不是放在.cpp文件中,因为编译过程中编译器需要看到完整的类定义。

模板必须在使用它们的每个翻译单元中定义;通过将函数定义放入源文件,它们只在一个单元中定义,如果您试图在另一个单元中使用它们,则会出现错误

将函数定义移到类定义之后的标题中