TimestampdConatiner.hp:
#ifndef TIMESTAMPEDCONTAINER_HPP_
#define TIMESTAMPEDCONTAINER_HPP_
using namespace std;
#include<string>
#include <ctime>
template <class k, class d>
class timestampedContainer
{
private:
struct elements
{
k keyType;
d dataType;
string timeStamp;
};
int position;
int size;
elements * containerPtr;
public:
timestampedContainer(int);
void insertElement(k,d);
void getElement(int, k, d, string);
void deleteContainer();
~timestampedContainer();
};
template<class k, class d>
timestampedContainer<k, d>::timestampedContainer(int size)
{
position = 0;
containerPtr = new elements[size];
}
template<class k, class d>
void timestampedContainer<k, d>::insertElement(k, d)
{
if(position <= size)
{
containerPtr[position] = elements(k, d);
position++;
}
}
#endif
当我试图将参数复制到元素结构数组中时,插入元素函数中会弹出错误。我叫它的方式有问题吗?这个错误到底意味着什么?
表达式elements(k, d)
存在两个问题。
- CCD_ 2和CCD_。因此,
elements(k, d)
根本没有意义 elements
没有显式定义的构造函数。因此,不能使用类似构造函数的调用来构造该类型的对象
您可能需要使用以下内容:
template<class kType, class dType>
void timestampedContainer<kType, dType>::insertElement(kType k, dType d)
{
if(position <= size)
{
containerPtr[position] = {k, d, ""};
position++;
}
}