如何在<double>矢量中创建复杂类型的模板<T>?

How can I create template of complex<double> type in vector<T>?

本文关键字:gt lt 类型 复杂 double 创建      更新时间:2023-10-16

我正在尝试使用如下所示的类创建数据结构,但是我在模板实现方面遇到了问题,因为该类型实际上很复杂。Sry 我没有使用正确的术语,而是使用如下所示的类代码:

template<class T>
class tableType
{
public:
string key;
vector<T> setValue;
~tableType();
tableType operator = (const tableType& obj) {
this->key = obj.key;
this->setValue = obj.setValue;
}
};

template<class typechosen>
class dataContainer :public tableType {
public:
vector<tableType<double>> ordNts;
vector<tableType<typechosen>> fields;
string type;
~dataContainer();
dataContainer operator = (const dataContainer& obj) {
this->ordNts = obj.ordNts;
}
};

例如,当我创建一个类变量时的示例数据类型,它应该是格式

dataContainer<complex<double>> dataStore;
dataStore.ordNts; //will be of type vector<tableType<double>>
dataStore.fields; //will be of type vector<tableType<complex<double>>>

如何实现这种类型的模板?

下面是一些工作代码:

#include <string>
#include <vector>
#include <complex>
template<class T>
class tableType
{
public:
std::string key;
std::vector<T> setValue;
};
template<class T>
class dataContainer
{
public:
std::vector<tableType<double>> ordNts;
std::vector<tableType<T>> fields;
std::string type;
};
int main()
{
dataContainer<std::complex<double>> dataStore;
auto j = dataStore.fields;
}

dataContainer不应该有基类(好吧,无论如何,不是基于您到目前为止发布的要求(。

还要检查您是否在 C++11 或更高版本模式下进行编译(C++>>11 中添加了使用令牌终止两个模板(。

如果您仍然遇到问题,请编辑问题以包含最小可重现示例。正如评论中所推测的那样,也许您错过了实际代码中的一些包含或std::,或者其他内容。

铌。类有一个隐式生成的operator=所以除非你需要不同的行为,否则你不应该编写自己的,这个类不应该,参见三/五/零规则。 同样,您可能不应该定义自己的析构函数。保持代码尽可能简单可以减少出错的机会,并更容易检查代码的正确性。