C++ 为什么使用vector获取链接器错误?

C++ 为什么使用vector获取链接器错误?,c++,stl,linker-errors,c++-standard-library,C++,Stl,Linker Errors,C++ Standard Library,我必须做家庭作业来写我自己的抽象类向量。我编写了一些代码,但当我试图编译它时,我出现了错误。代码如下: vector.hh: #ifndef VECTOR__HH__ #define VECTOR__HH_ template<class T> class Vector { int capacity_; int size_; T* buffer_; void ensure_capacity(unsigned size); public: V

我必须做家庭作业来写我自己的抽象类向量。我编写了一些代码,但当我试图编译它时,我出现了错误。代码如下:

vector.hh

#ifndef VECTOR__HH__
#define VECTOR__HH_

template<class T> class Vector {
    int capacity_;
    int size_;
    T* buffer_;

    void ensure_capacity(unsigned size);

public:
    Vector(int capacity=10) 
      : capacity_(capacity), size_(0), buffer_(new T[capacity])
    { }

    ~Vector() {
        delete []buffer_;
    }

    int size() const {
        return size_;
    }

    bool empty() const {
        return size_ == 0;
    }

    T& operator[](int n) {
        return buffer_[n];
    }

    const T& operator[](int n) const {
        return buffer_[n];
    }

    void clear() {
        // TODO
    }

    int capacity() const {
        return capacity_;
    }

    T& front() {
        return buffer_[0];
    }

    const T& front() const {
        return buffer_[0];
    }

    T& back() {
        return buffer_[size_-1];
    }

    const T& back() const {
        return buffer_[size_-1];
    }

    void push_back(const T& value);
};
#endif
#include "vector.hh"

template<class T> 
void Vector<T>::ensure_capacity(unsigned size) {
    if(capacity_>size+1) {
        return;
    } 

    capacity_ = capacity_ ==0?1:capacity_;

    while(capacity_<size+1) {
        capacity_*=2;
    }

    T* old_buffer = buffer_;
    buffer_ = new T[capacity_];
    memcpy(buffer_, old_buffer, sizeof(T)*size_);

    delete [] old_buffer;
}

template<class T>
void Vector<T>::push_back(const T& value) {
    ensure_capacity(size()+1);

    buffer_[size_] = value;
    size_++;
    buffer_[size_] = '/0';
}
#include "vector.hh"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    Vector<int> v(2);
    v.push_back(10);

    cout << v[0];

    return 0;
}
#ifndef向量uuhh__
#定义向量_
模板类向量{
国际能力(int capacity);
int-size_389;;
T*缓冲区;
无效容量(无符号大小);
公众:
向量(整数容量=10)
:容量(容量)、大小(0)、缓冲区(新的[容量])
{ }
~Vector(){
删除[]缓冲区;
}
int size()常量{
返回大小;
}
bool empty()常量{
返回大小==0;
}
T&operator[](内部编号){
返回缓冲区_n;
}
常量T和运算符[](整数n)常量{
返回缓冲区_n;
}
无效清除(){
//待办事项
}
int capacity()常量{
返回容量;
}
T&front(){
返回缓冲区[0];
}
常数T&前()常数{
返回缓冲区[0];
}
T&back(){
返回缓冲区[size_u1];
}
常量T&back()常量{
返回缓冲区[size_u1];
}
无效推回(常数T和值);
};
#恩迪夫
vector.cc

#ifndef VECTOR__HH__
#define VECTOR__HH_

template<class T> class Vector {
    int capacity_;
    int size_;
    T* buffer_;

    void ensure_capacity(unsigned size);

public:
    Vector(int capacity=10) 
      : capacity_(capacity), size_(0), buffer_(new T[capacity])
    { }

    ~Vector() {
        delete []buffer_;
    }

    int size() const {
        return size_;
    }

    bool empty() const {
        return size_ == 0;
    }

    T& operator[](int n) {
        return buffer_[n];
    }

    const T& operator[](int n) const {
        return buffer_[n];
    }

    void clear() {
        // TODO
    }

    int capacity() const {
        return capacity_;
    }

    T& front() {
        return buffer_[0];
    }

    const T& front() const {
        return buffer_[0];
    }

    T& back() {
        return buffer_[size_-1];
    }

    const T& back() const {
        return buffer_[size_-1];
    }

    void push_back(const T& value);
};
#endif
#include "vector.hh"

template<class T> 
void Vector<T>::ensure_capacity(unsigned size) {
    if(capacity_>size+1) {
        return;
    } 

    capacity_ = capacity_ ==0?1:capacity_;

    while(capacity_<size+1) {
        capacity_*=2;
    }

    T* old_buffer = buffer_;
    buffer_ = new T[capacity_];
    memcpy(buffer_, old_buffer, sizeof(T)*size_);

    delete [] old_buffer;
}

template<class T>
void Vector<T>::push_back(const T& value) {
    ensure_capacity(size()+1);

    buffer_[size_] = value;
    size_++;
    buffer_[size_] = '/0';
}
#include "vector.hh"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    Vector<int> v(2);
    v.push_back(10);

    cout << v[0];

    return 0;
}
#包括“vector.hh”
模板
无效向量::确保容量(无符号大小){
如果(容量>大小+1){
返回;
} 
容量=容量=0?1:容量;

在模板编程中,函数的定义应该在定义类模板的地方可见。这通常是通过在类本身内部定义函数来完成的

因此,有两种方法可以解决您的问题:

  • 将所有定义从
    vector.cpp
    移动到
    vector.hh
    (这实际上是通常的解决方案)。并在不需要时删除
    vector.cpp
  • 或者,在定义
    vector
    类模板后,在
    vector.hh
    文件末尾包含
    vector.cpp
    文件,如下所示:

    #ifndef VECTOR__HH__
    #define VECTOR__HH__  //<--- corrected this also!
    
    template<class T> 
    class Vector {
       //...
    };
    
    //...
    
    #include "vector.cpp"
    
    #endif VECTOR__HH__
    
    #ifndef向量uuhh__
    
    #定义向量\uuuhh\uuuu/链接器错误即将出现,因为对于
    模板
    类,定义应始终可见。您可以将
    VECTOR.cc
    的所有内容移动到
    VECTOR.h
    中。或者您可以在包含
    VECTOR.h
    的任何位置包含
    VECTOR.cc

    旁注

    #ifndef VECTOR__HH__
    #define VECTOR__HH_
    
    template<class T> class Vector {
        int capacity_;
        int size_;
        T* buffer_;
    
        void ensure_capacity(unsigned size);
    
    public:
        Vector(int capacity=10) 
          : capacity_(capacity), size_(0), buffer_(new T[capacity])
        { }
    
        ~Vector() {
            delete []buffer_;
        }
    
        int size() const {
            return size_;
        }
    
        bool empty() const {
            return size_ == 0;
        }
    
        T& operator[](int n) {
            return buffer_[n];
        }
    
        const T& operator[](int n) const {
            return buffer_[n];
        }
    
        void clear() {
            // TODO
        }
    
        int capacity() const {
            return capacity_;
        }
    
        T& front() {
            return buffer_[0];
        }
    
        const T& front() const {
            return buffer_[0];
        }
    
        T& back() {
            return buffer_[size_-1];
        }
    
        const T& back() const {
            return buffer_[size_-1];
        }
    
        void push_back(const T& value);
    };
    #endif
    
    #include "vector.hh"
    
    template<class T> 
    void Vector<T>::ensure_capacity(unsigned size) {
        if(capacity_>size+1) {
            return;
        } 
    
        capacity_ = capacity_ ==0?1:capacity_;
    
        while(capacity_<size+1) {
            capacity_*=2;
        }
    
        T* old_buffer = buffer_;
        buffer_ = new T[capacity_];
        memcpy(buffer_, old_buffer, sizeof(T)*size_);
    
        delete [] old_buffer;
    }
    
    template<class T>
    void Vector<T>::push_back(const T& value) {
        ensure_capacity(size()+1);
    
        buffer_[size_] = value;
        size_++;
        buffer_[size_] = '/0';
    }
    
    #include "vector.hh"
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[]) {
        Vector<int> v(2);
        v.push_back(10);
    
        cout << v[0];
    
        return 0;
    }
    
    .h文件中的以下行没有帮助:

    #ifndef VECTOR__HH__
    #define VECTOR__HH_
    

    使两个宏相似,例如,
    VECTOR\uuuhh
    。此宏用于避免包含多个文件。

    可能会有所帮助。+1有助于在卡住时实际进行努力并提出问题,而不是简单地询问代码!:)请注意,双下划线是为库实现程序保留的。
    #define
    中有一个错误。在问题中,您可以在代码中更正它。