我正在写一个序列化和反序列化的方法,并且我在执行deserialize
时遇到了一个问题:我不能new
a value_type
,实际上它是Skill*
。如何新增value_type指针?
template <class T >
static istream &DeSerializePVector(istream& istream_, T& container)
{
typedef typename T::value_type ElementType;
size_t size;
istream_ >> size;
container.reserve(size);
container.resize(size);
for(typename T::iterator ite = container.begin(); ite != container.end(); ite++)
{
*ite = new *ElementType; //how can I initialize this type?
(*ite)->DeSerialize(istream_);
}
return istream_;
}
int main()
{
Skill* disease = Factory::CreateSkill (SKILLTYPE_DISEASE);
Skill* purify = Factory::CreateSkill (SKILLTYPE_PURIFY);
Skill* skills[2] = {disease, purify};
vector<Skill*> int_vector = Tools::MakeVector (skills);
ofstream fileOut;
fileOut.open ("data.txt", std::ofstream::binary);
ISerializable::SerializePVector(fileOut, int_vector);
fileOut.flush();
fileOut.close();
ifstream fileIn;
vector<Skill*> int_vector2;
fileIn.open ("data.txt", std::ofstream::binary);
ISerializable::DeSerializePVector(fileIn, int_vector2);
}
我该如何获得这项工作?
正确的语法是'new ElementType'(没有“new”和“ElementType”之间的星号)。这是问题吗?或者你问如何决定使用哪个构造函数? '新的ElementType'意味着你调用默认的构造函数。 – jogojapan
据我可以告诉代码,你已经分配了这些对象。 * ite无论是疾病还是净化。在这种情况下,您根本不需要'* ite = new ElementType'这一行,只需调用DeSerialize即可,我认为这实际上是用数据填充这些对象。 – combinatorial
我不能新的技能* ..如果一个技能放在向量中,那么你是对的。 –