如何使用 void 函数从 class 生成数组
How to make an array from a class, with the void function
问题是我正在制作一个程序,该程序将使用 5 个银行帐户,用户在其中输入 5 组不同的名称和金额。它将获取这些集合,将它们放入一个数组中。并显示它们。
我正在设置一个数组,它将要求输入 3 个输入、名字、姓氏和金额。它会将它们放入每个数组中。但是当我尝试调用它时,出现错误。
我试图通过尝试做 void readCustomer(bankAccount users[]);
来调用它,但这也不起作用。我不知道怎么称呼它。
const int ARR_SIZE = 5;
class bankAccount{
private:
string firstname, lastname, initials;
int accountNum, amount;
public:
void readCustomer(); //will get inputs and store them
into an array.
};
int main(){
bankAccount users[ARR_SIZE];
for (i = 0; i < ARR_SIZE; i++){
users[i].readCustomer();
}
}
void bankAccount::readCustomer(){
amount = 0;
for(i = 0; i < ARR_SIZE; i++){
cout << "Reading data for customer" << endl;
cout << "First Name: ";
cin >> users[i].firstname;
cout << endl;
cout << "Last Name: ";
cin >> users[i].lastname;
cout << endl;
cout << "Amount: ";
cin >> users[i].amount;
cout << endl;
}
}
我希望得到 cout
s 和 cin
s 来调用要放入数组中的名字和姓氏。但是我得到这个错误:
In function 'int main()':
16:8: error: request for member 'readCustomer' in 'users', which is of non-class type 'bankAccount [5]'
16:33: error: expected primary-expression before 'users'
我不知道这意味着什么。
首先,我建议您使用 C++ 容器而不是 C 风格的数组。比如一个std::vector
。例如,使用矢量可以让您在 运行 时间更改帐户总数,而不是现在的固定大小(又名 5)。
但是您的代码的根本问题是相同的,因此此答案也将使用 c 样式数组。
问题是 (IMO) 设计问题。当您创建一个名为 bankAccount
的 class 时,它将代表 一个 帐户。换句话说,这样的 class 应该对数组一无所知。然后你可以创建另一个代表帐户集合的 class 或者简单地创建一个帐户数组。
它可能看起来像:
class bankAccount{
public:
string firstname, lastname, initials;
int accountNum, amount;
void readCustomer(); // no argument
};
int main(){
bankAccount users[ARR_SIZE];
for (int i=0; i<ARR_SIZE; ++i)
{
users[i].readCustomer();
// ^^^^^^^^
// This part gets you the bankAccount instance at index i of the array.
// The ".readCustomer()" then calls the function on that specific
// bankAccount instance
}
}
void bankAccount::readCustomer(){
// read/initialize information for the account
// Example:
// Initialize amount to zero
amount = 0;
}
问题是我正在制作一个程序,该程序将使用 5 个银行帐户,用户在其中输入 5 组不同的名称和金额。它将获取这些集合,将它们放入一个数组中。并显示它们。 我正在设置一个数组,它将要求输入 3 个输入、名字、姓氏和金额。它会将它们放入每个数组中。但是当我尝试调用它时,出现错误。
我试图通过尝试做 void readCustomer(bankAccount users[]);
来调用它,但这也不起作用。我不知道怎么称呼它。
const int ARR_SIZE = 5;
class bankAccount{
private:
string firstname, lastname, initials;
int accountNum, amount;
public:
void readCustomer(); //will get inputs and store them
into an array.
};
int main(){
bankAccount users[ARR_SIZE];
for (i = 0; i < ARR_SIZE; i++){
users[i].readCustomer();
}
}
void bankAccount::readCustomer(){
amount = 0;
for(i = 0; i < ARR_SIZE; i++){
cout << "Reading data for customer" << endl;
cout << "First Name: ";
cin >> users[i].firstname;
cout << endl;
cout << "Last Name: ";
cin >> users[i].lastname;
cout << endl;
cout << "Amount: ";
cin >> users[i].amount;
cout << endl;
}
}
我希望得到 cout
s 和 cin
s 来调用要放入数组中的名字和姓氏。但是我得到这个错误:
In function 'int main()': 16:8: error: request for member 'readCustomer' in 'users', which is of non-class type 'bankAccount [5]' 16:33: error: expected primary-expression before 'users'
我不知道这意味着什么。
首先,我建议您使用 C++ 容器而不是 C 风格的数组。比如一个std::vector
。例如,使用矢量可以让您在 运行 时间更改帐户总数,而不是现在的固定大小(又名 5)。
但是您的代码的根本问题是相同的,因此此答案也将使用 c 样式数组。
问题是 (IMO) 设计问题。当您创建一个名为 bankAccount
的 class 时,它将代表 一个 帐户。换句话说,这样的 class 应该对数组一无所知。然后你可以创建另一个代表帐户集合的 class 或者简单地创建一个帐户数组。
它可能看起来像:
class bankAccount{
public:
string firstname, lastname, initials;
int accountNum, amount;
void readCustomer(); // no argument
};
int main(){
bankAccount users[ARR_SIZE];
for (int i=0; i<ARR_SIZE; ++i)
{
users[i].readCustomer();
// ^^^^^^^^
// This part gets you the bankAccount instance at index i of the array.
// The ".readCustomer()" then calls the function on that specific
// bankAccount instance
}
}
void bankAccount::readCustomer(){
// read/initialize information for the account
// Example:
// Initialize amount to zero
amount = 0;
}