我正在尝试序列化帐户向量,但我不断收到以下异常:-
要实现 XML 可序列化,从 ICollection 继承的类型必须在其继承层次结构的所有级别上都有 Add(Account) 的实现。cliext.vector 没有实现 Add(Account)。
我在课堂上添加了一个 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;
}
帐户类
帐户.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);
};
帐号.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;
}
帐户列表.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;
}