我正在尝试序列化一个帐户向量,但我一直得到以下异常: -
要进行XML序列化,从ICollection继承的类型必须在其继承层次结构的所有级别都具有Add(Account)的实现。 cliext.vector没有实现Add(Account)。
我已经为我的班级添加了一个添加(帐户)功能,但它仍然无法正常工作。我对C ++比较陌生,所以我的代码可能很差,或者可能不是我想要的正确方法。
所有帮助表示赞赏。
这是主要功能: -
// TestDotNetXML.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Serialization;
int main(array<System::String ^> ^args)
{
AccountList^ MyAccountList = gcnew AccountList;
Account^ Account1 = gcnew Account("1","1","1","1");
Account^ Account2 = gcnew Account("2","2","2","2");
Account Account3;
Account3.setAccountName("3");
Account3.setUserName("3");
Account3.setPassword("3");
Account3.setDescription("3");
MyAccountList->Add(Account1);
MyAccountList->Add(Account2);
MyAccountList->Add(Account3);
try
{
XmlSerializer^ x = gcnew XmlSerializer(MyAccountList->GetType());
x->Serialize(Console::Out, MyAccountList);
}
catch(System::InvalidOperationException^ ex)
{
Console::WriteLine(ex->InnerException);
}
Console::WriteLine();
Console::ReadLine();
return 0;
}
帐户类
Account.h
#pragma once
using namespace System;
[Serializable]
public ref class Account
{
public:
String^ accountName;
String^ userName;
String^ password;
String^ description;
public:
Account(String^ accountName, String^ userName, String^ password, String^ description);
Account(void);
Account(const Account% ob);
Account% operator=(const Account% ob);
~Account(void);
String^ getAccountName();
void setAccountName(String^ accountName);
String^ getUserName();
void setUserName(String^ userName);
String^ getPassword();
void setPassword(String^ password);
String^ getDescription();
void setDescription(String^ description);
};
Account.cpp
#include "stdafx.h"
#include "Account.h"
Account::Account(String^ accountName, String^ userName, String^ password, String^ description)
{
this->accountName = gcnew String(accountName);
this->userName = gcnew String(userName);
this->password = gcnew String(password);
this->description = gcnew String(description);
}
Account::Account(void)
{
}
Account::Account(const Account %ob)
{
accountName = ob.accountName;
userName = ob.userName;
password = ob.password;
description = ob.description;
}
Account% Account::operator=(const Account% ob)
{
if(this != %ob)
{
accountName = ob.accountName;
userName = ob.userName;
password = ob.password;
description = ob.description;
}
return *this;
}
Account::~Account(void)
{
this->accountName = gcnew String("");
this->userName = gcnew String("");
this->password = gcnew String("");
this->description = gcnew String("");
}
String^ Account::getAccountName()
{
return this->accountName;
}
void Account::setAccountName(String^ accountName)
{
this->accountName = accountName;
}
String^ Account::getUserName()
{
return this->userName;
}
void Account::setUserName(String^ userName)
{
this->userName = accountName;
}
String^ Account::getPassword()
{
return this->password;
}
void Account::setPassword(String^ password)
{
this->password = password;
}
String^ Account::getDescription()
{
return this->description;
}
void Account::setDescription(String^ description)
{
this->description = description;
}
AccountList.h
#pragma once
#include <cliext/vector>
#include "Account.h"
using namespace cliext;
public ref class AccountList
{
public:
vector<Account^>^ List;
public:
AccountList(void);
void Add(Account^ anAccount);
void Add(Account anAccount);
void Remove(Account^ anAccount);
bool Exists(Account^ anAccount);
Account^ getAccout(String^ accountName);
vector<Account^>^ getList(void);
unsigned int stopLNK2022() { return List->size(); }
};
AccountList.cpp
#include "stdafx.h"
#include "AccountList.h"
#include "Account.h"
AccountList::AccountList(void)
{
List = gcnew vector<Account^>;
}
void AccountList::Add(Account^ anAccount)
{
this->List->push_back(anAccount);
}
void AccountList::Add(Account anAccount)
{
Account^ ptrAccount = gcnew Account(
anAccount.getAccountName(),
anAccount.getUserName(),
anAccount.getPassword(),
anAccount.getDescription()
);
this->List->push_back(ptrAccount);
}
void AccountList::Remove(Account^ anAccount)
{
vector<Account^>::iterator Iter;
for(Iter = List->begin(); Iter != List->end(); Iter++)
{
if((safe_cast<Account^>(*Iter))->getAccountName()
== anAccount->getAccountName())
{
List->erase(Iter);
}
}
}
bool AccountList::Exists(Account^ anAccount)
{
vector<Account^>::iterator Iter;
for(Iter = List->begin(); Iter != List->end(); Iter++)
{
if((safe_cast<Account^>(*Iter))->getAccountName()
== anAccount->getAccountName())
{
return true;
}
}
return false;
}
vector<Account^>^ AccountList::getList(void)
{
return List;
}
Account^ AccountList::getAccout(String^ accountName)
{
vector<Account^>::iterator Iter;
for(Iter = List->begin(); Iter != List->end(); Iter++)
{
String^ test = (safe_cast<Account^>(*Iter))->getAccountName();
if((safe_cast<Account^>(*Iter))->getAccountName()
== accountName)
{
return safe_cast<Account^>(*Iter);
}
}
return nullptr;
}