【发布时间】:2014-12-06 03:51:51
【问题描述】:
我有一个 BankAccount 类,它的一个私有成员是 BankAccount* 客户 [10]。我想将 BankAccount(string s, int a, double c, double r, double s) 类型的对象放在数组中,然后使用 getString() 访问每个单独的数据,例如使用 getString() 访问 string s 或使用 getAccountNum( )。一直在尝试很多东西,但我得到一个空白的黑匣子。如何将对象放入私有类数组中?
#include "BankAccount.h"
BankAccount::BankAccount() : customers()
{
}
void BankAccount::work()
{
string n = "name";
string* nn = &n;
BankAccount* b = new BankAccount(nn);
customers[0] = b;
go();
}
BankAccount::BankAccount(string* n)
{
name = n;
}
string BankAccount::getName()
{
return *name;
}
void BankAccount::go()
{
string st = customers[0]->getName();
cout << st << endl;
}
【问题讨论】:
-
你的代码有更多的问题,我想我害怕看到其余的。 :)
-
BankAccount* b = new BankAccount(); b = new BankAccount(n); BankAccount a[1]; a[0] = *b;哦,我的。 -
你真的需要再次阅读你的 C++ 书中关于类的章节。你还没有得到它。
-
我现在收到一个写入违规错误。我将如何正确实施?我已经为此工作了几个小时。我只想将类对象存储在数组中。
-
@WilliamWymerus - 你的代码有很多问题。这种情况的一个症状是“写作违规”。
标签: c++ arrays pointers object stack-overflow