c++ - 引用不足
问题描述
我正在学习模板,我无法编译这段代码。错误是“未定义对 `SimpleVector::SimpleVector(int)' 的引用”(以及 main.cpp 中与 SimpleVector 相关的所有其他内容都有此错误)。如果我将 cpp 文件的所有代码放在头文件中,它仍然可以工作,并且我确定它们在同一个目录中。
SimpleVector.h 文件:
#ifndef SIMPLEVECTOR_H_INCLUDED
#define SIMPLEVECTOR_H_INCLUDED
template <class T>
class SimpleVector
{
private:
T *aptr;
int arraySize;
void memError();
void subError();
public:
SimpleVector()
{
aptr = 0;
arraySize = 0;
}
SimpleVector(int);
SimpleVector(const SimpleVector &);
~SimpleVector();
int size() const
{
return arraySize;
}
T getElementAt(int position);
T &operator[](const int &);
void push_back(T);
void pop_back();
};
#endif // SIMPLEVECTOR_H_INCLUDED
SimpleVector.cpp 文件:
#include "SimpleVector.h"
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
for (int count = 0; count < arraySize; count++)
{
*(aptr + count) = 0;
}
}
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
arraySize = obj.arraySize;
aptr = new T [arraySize];
if (aptr == 0) memError();
for (int count = 0; count < arraySize; count++)
{
*(aptr + count) = *(obj.aptr + count);
}
}
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
template <class T>
void SimpleVector<T>::memError()
{
cout << "ERROR: Cannot allocate memory.\n";
exit(EXIT_FAILURE);
}
template <class T>
void SimpleVector<T>::subError()
{
cout << "ERROR: Subcript out of range.\n";
exit(EXIT_FAILURE);
}
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
template <class T>
T &SimpleVector<T>::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
template <class T>
void SimpleVector<T>::push_back(T num)
{
T *temp;
temp = new T [arraySize];
for (int count = 0; count < arraySize; count ++)
temp[count] = aptr[count];
delete [] aptr;
arraySize += 1;
aptr = new T [arraySize];
for (int i = 0; i < arraySize; i++)
{
aptr[i] = temp[i];
}
aptr[arraySize-1] = num;
delete [] temp;
}
template <class T>
void SimpleVector<T>::pop_back()
{
T *temp;
temp = new T [arraySize];
for (int count = 0; count < arraySize; count ++)
temp[count] = aptr[count];
delete [] aptr;
arraySize -= 1;
aptr = new T [arraySize];
for (int i = 0; i < arraySize; i++)
{
aptr[i] = temp[i];
}
delete [] temp;
}
main.cpp 文件:
#include <iostream>
#include "SimpleVector.h"
using namespace std;
int main()
{
const int SIZE = 10;
int count;
SimpleVector<int> intTable(SIZE);
SimpleVector<double> doubleTable(SIZE);
for (count = 0; count < SIZE; count ++)
{
intTable[count] = (count * 2);
doubleTable[count] = (count * 2.14);
}
cout << "These values are in intTable: \n";
for (count = 0; count < SIZE; count ++)
cout << intTable[count] << " ";
cout << endl;
cout << "These values are in doubleTable: \n";
for (count = 0; count < SIZE; count ++)
cout << doubleTable[count] << " ";
cout << endl;
cout << "\nPop_back intTable and doubleTable.\n";
intTable.pop_back();
doubleTable.pop_back();
cout << "These values are in intTable: \n";
for (count = 0; count < intTable.size(); count ++)
cout << intTable[count] << " ";
cout << endl;
cout << "These values are in doubleTable: \n";
for (count = 0; count < doubleTable.size(); count ++)
cout << doubleTable[count] << " ";
cout << endl;
cout << "\nPush_back intTable and doubleTable.\n";
intTable.push_back(18);
doubleTable.push_back(9*2.14);
cout << "These values are in intTable: \n";
for (count = 0; count < intTable.size(); count ++)
cout << intTable[count] << " ";
cout << endl;
cout << "These values are in doubleTable: \n";
for (count = 0; count < doubleTable.size(); count ++)
cout << doubleTable[count] << " ";
cout << endl;
return 0;
}
解决方案
推荐阅读
- asp.net - Process.start() 在 iis 上不起作用
- mysql - ERROR 1064 (42000): You have an error in your SQL - when trying to insert a response json into my mysql
- java - 为什么有必要明确说 lambda 是 Consumer 以使用 andThen 方法?
- javascript - 从输入中突出显示多个内容 div 中的文本
- javascript - 设备令牌过期时,是否会自动从 FCM 设备组中删除?
- java - 我想要java正则表达式所有匹配的索引
- c# - C# WPF LiveChart 在动态创建的图表中更新数据系列
- mongodb - 从 mongo db 中相同结构的多个集合中获取数据
- javascript - 热链接图像使用 React 和 Webpack 渲染得非常慢
- python - 404 错误的 Django Rest Framework 自定义消息