报错内容如下:
错误 LNK2019 无法解析的外部符号 "private: void __thiscall myVector<int>::copy_from(int const *,int,int)" (?copy_from@?$myVector@H@@AAEXPBHHH@Z),该符号在函数 "public: __thiscall myVector<int>::myVector<int>(int const *,int)" (??0?$myVector@H@@QAE@PBHH@Z) 中被引用 ConsoleApplication1 C:\Users\qwer\source\repos\ConsoleApplication1\ConsoleApplication1.obj 1
这个是头文件:
#include<iostream>
using namespace std;
const int DEFAULT_CAPACITY = 3;
using Rank = int;//秩
template<class T>
class increase_operation
{
public:
void operator()(T& e)
{
e++;
}
};
template<class T> class myVector
{
friend class increase_operation<T>;
public:
//构造函数
myVector(int default_capacity = DEFAULT_CAPACITY, int default_size = 0, T default_elem = 0)//
{
_capacity = default_capacity;
_elem = new T[_capacity];
for (_size = 0; _size < default_size; _size++)
{
_elem[_size] = default_elem;
}
}
myVector(const T* elem, Rank n)
{
copy_from(elem, 0, n);
}
myVector(const T* elem, Rank low, Rank high)
{
copy_from(elem, low, high);
}//在调用的时候的构造函数
myVector(const myVector<T> & elem)
{
copy_from(elem._elem, 0, elem._size);
}
myVector(const myVector<T> & elem, Rank low, Rank high)
{
copy_from(elem._elem, low, high);
}
//析构函数
~myVector()
{
delete[] _elem;
}
//接口
//重载运算符
myVector& operator=(myVector<T> const& V)
{
if (_elem)
{
delete[] _elem;
}
copy_from(V._elem, 0, V._size);
}
T& operator[](Rank r)
{
return _elem[r];
}
//功能
Rank insert(Rank, T const&);
int remove(Rank, Rank);
T remove(Rank);
Rank find(T const&, Rank, Rank) const;
void unsort(Rank lo, Rank hi);
int deduplicate();
template<class tx>
void traverse(tx visit)
{
for (int i = 0; i < this->_size; i++)
{
visit(this->_elem[i]);
}
}
private:
//数据存放
Rank _size = 0;//存放了的元素总数
int _capacity = 0;//容器的大小
T* _elem;//具体的元素内容
//内置实现
void copy_from(const T* A, Rank low, Rank high);
void expand();
void shrink();
void permute(myVector<T>&, Rank low = 0, Rank high = this->_size)
{
for (Rank i = low; i < high; i++)
{
swap(this->_elem[i], this->_elem[rand() % i]);
}
}
};
copy_from函数定义如下:
template<class T>
void myVector<T>::copy_from(const T* elem, Rank low, Rank high)
{
this->_capacity = (high - low) << 1;
this->_elem = new T[_capacity];
this->_size = 0;
while (high > low)
{
this->_elem[_size] = elem[low];
_size++;
low++;
}
}
有大佬知道是怎么了吗?谢谢。