【发布时间】:2020-01-13 23:35:08
【问题描述】:
问题是我正在制作一个程序,它需要 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;
}
}
我期待 couts 和 cins 要求将名字、姓氏放入数组中。但我得到这个错误:
在函数'int main()'中: 16:8:错误:在“users”中请求成员“readCustomer”,它是非类类型“bankAccount [5]” 16:33:错误:“用户”之前的预期主表达式
我不知道这意味着什么。
【问题讨论】:
-
错误消息的字面意思是“您正在尝试调用数组上的方法,我不知道该怎么做。”要解决眼前的问题: 1) 将
readCustomerstatic 命名为bankAccount::readCustomer(users)(注意::而不是.)或将readCustomer设置为bankAccount之外的常规函数。 2) readCustomer 不知道它接收到的数组的大小。您应该单独传递大小,或者使用更智能的集合,例如std::vector<bankAccount>或std::array<bankAccount, ARR_SIZE> -
您需要在您最喜欢的 C++ 书籍中阅读更多关于如何编写和使用函数以及数组的基础知识。
标签: c++ arrays class constructor destructor