在尝试编译为代码时,我收到以下错误:
Error 5 error LNK1120: 2 unresolved externals C:\Users\---\VSworkspace\Work\Debug\AbstractTypes.exe AbstractTypes
Error 3 error LNK2019: unresolved external symbol "private: void __thiscall Vec<int>::create(void)" (?create@?$Vec@H@@AAEXXZ) referenced in function "public: __thiscall Vec<int>::Vec<int>(void)" (??0?$Vec@H@@QAE@XZ) C:\Users\---\VSworkspace\Work\AbstractTypes\main.obj AbstractTypes
Error 4 error LNK2019: unresolved external symbol "private: void __thiscall Vec<int>::uncreate(void)" (?uncreate@?$Vec@H@@AAEXXZ) referenced in function "public: __thiscall Vec<int>::~Vec<int>(void)" (??1?$Vec@H@@QAE@XZ) C:\Users\---\VSworkspace\Work\AbstractTypes\main.obj AbstractTypes
这是代码
Vec.h
#pragma once
#ifndef GUARD_VEC_H
#define GUARD_VEC_H
#include <memory>
#include <cstddef> //FOR: size_type
template<class T> class Vec {
public: //interface
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& operator = (const Vec&);
~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; }
iterator begin() { return data; }
const_iterator begin() const { return data; }
iterator end() { return avail; }
const_iterator end() const { return avail; }
private: //implementation
iterator data;
iterator avail;
iterator limit;
std::allocator<T> alloc;
void create();
void create(size_type, const T&);
void create(const_iterator, const_iterator);
void uncreate();
void grow();
void unchecked_append(const T&);
};
#endif
Vec.cpp
#include "Vec.h";
#include <memory>
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){
iterator it = avail;
while (it != data)
alloc.destroy(--it);
alloc.deallocate(data, limit - data);
}
data = limit = avail = 0;
}
template <class T> void Vec<T>::grow()
{
size_type new_size = max(2 * (limit - data), ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
template <class T> void Vec<T>::unchecked_append(const T& val)
{
alloc.construct(avail++, val);
}
* Main.cpp的**
#include "Vec.h"
#include <iostream>
int main(){
Vec<int> vector;
return 0;
}
我对这个错误做了一些研究,可能与这个模板类的事实有关。我祈祷这可能不是一个愚蠢的错误。非常感谢,谢谢。