首页 > 解决方案 > 将参数复制到结构元素数组时,“)”标记之前的主表达式

问题描述

TimestampedConatiner.hpp:

#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

当我尝试将参数复制到元素结构数组中时,插入元素函数中会弹出错误。我打电话的方式有问题吗?这个错误到底是什么意思?

标签: c++

解决方案


表达式有两个问题elements(k, d)

  1. k并且d是类型。因此,elements(k, d)根本没有意义。
  2. elements没有明确定义的构造函数。因此,您不能使用类似构造函数的调用来构造该类型的对象。

您可能想要使用类似的东西:

template<class kType, class dType>
void timestampedContainer<kType, dType>::insertElement(kType k, dType d)
{
    if(position <= size)
    {
        containerPtr[position] = {k, d, ""};
        position++;
    }
}

推荐阅读