我正在制作Vector模板类并遇到错误 LNK2019:未解析的外部符号“public:__thiscall SimpleVector :: SimpleVector(int)”(?? 0?$ SimpleVector @ H @@ QAE @ H @ Z)在函数_main 中引用 每当我尝试调用main中的任何函数时。我研究过我可以在main.cpp文件中包含函数定义.cpp文件,或者在我的函数定义.cpp中包含main,并且这两种方法都解决了问题,但我认为实际上并没有修复代码中的底层问题。我的问题是我的代码中是否存在任何导致此问题的代码。
SimpVec.h
#ifndef SIMPLEVECTOR
#define SIMPLEVECTOR
template <class T>
class SimpleVector
{
private:
T* aptr;
int arraySize;
public:
SimpleVector();
SimpleVector(int);
SimpleVector(const SimpleVector&);
~SimpleVector();
int size() const { return arraySize; }
T getElementAt(int position);
T& operator[](int);
void pushBack(double);
};
#endif SIMPLEVECTOR// !SIMPLEVECTOR
SimpVecFunc.cpp
#include "SimpVec.h"
#include <iostream>
template <class T>
SimpleVector<T>::SimpleVector()
{
aptr(0);
arraySize(0);
}
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
aptr = new T[s];
std::cout<<"Array created"<<std::endl;
}
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector& v)
{
arraySize = v.arraySize;
aptr = new T[arraySize];
for (int i = 0; i < arraySize; i++)
*(aptr + i) = *(v.aptr + i);
}
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete[] aptr;
}
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
return aptr[sub];
}
template <class T>
T& SimpleVector<T>::operator[](int sub)
{
return aptr[sub];
}
template <class T>
void SimpleVector<T>::pushBack(double Val)
{
for (int index = 0, index++)
{
if (this->aptr[i] = 0)
{
this->aptr[i] = Val;
std::cout << "Added to current array size not change need" << std::endl;
return
}
if (index < this->arraysize)
{
SimpleVector<T> temP();
temP.arraySize = this->arraySize + 1;
aptr = new T[arraySize];
for (int i = 0; i < arraySize; i++)
{
*(aptr + i) = *(v.aptr + i);
}
temP.aptr[this->arraySize] = Val;
std::cout << "Array was resized and pushd back" << std::endl;
return;
}
}
}
Driver.cpp
#include "SimpVec.h"
void main()
{
SimpleVector<int> TEST1(3);
}