我试图使用如下图所示的类创建一个数据基础架构,但我在模板实现方面遇到了问题,因为类型实际上是复杂的。很抱歉,我没有使用正确的术语,但下面所示的类的代码。
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::
在真正的代码中,或什么。
NB. 类有一个隐式生成的 operator=
所以你不应该写你自己的,除非你需要不同的行为,这个类不应该,见 三五零规则. 同样,你可能也不应该定义自己的析构器。尽可能的保持代码的简单性,减少出错的机会,并使其更容易审查代码的正确性。