【问题标题】:How to make an array from a class, with the void function如何使用 void 函数从类中创建数组
【发布时间】: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&lt;bankAccount&gt;std::array&lt;bankAccount, ARR_SIZE&gt;
  • 您需要在您最喜欢的 C++ 书籍中阅读更多关于如何编写和使用函数以及数组的基础知识。

标签: c++ arrays class constructor destructor


【解决方案1】:

首先,我建议您使用 c++ 容器而不是 c 样式的数组。例如std::vector。例如,使用向量可以让您在运行时更改帐户总数,而不是您现在拥有的固定大小(又名 5)。

但是您的代码的基本问题是相同的,因此此答案也将使用 c 样式数组。

问题是 (IMO) 设计问题。当你创建一个名为bankAccount 的类时,它应该代表一个帐户。换句话说,这样的类不应该有关于数组的知识。然后您可以创建另一个代表帐户集合的类或简单地创建一个帐户数组。

它可能看起来像:

   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;
   }

【讨论】:

  • 所以我尝试将其更改为您的方式,但我尝试在 readCustomer 中执行 for 循环,执行 cin >> users[i].firstname 等。但是我收到一个错误,告诉我它没有在范围内声明。 **另外,bankAccouint 的字符串和 int 是私有的,不是公开的。我很抱歉。
  • @lowmask2 好吧,正如我在答案中所写:readCustomer 应该知道数组 (IMO)。所以不要在readCustomer 内循环。只需读取单个客户的数据。换句话说:不要做cin &gt;&gt; users[i].firstname,而只是做cin &gt;&gt; firstname。如果你真的想要一个循环读取所有帐户的函数,那可以做到。但在这种情况下,您不会将函数放在class bankAccount 中,而是放在另一个代表bankAccount 的集合(又名数组/向量/列表)的类中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 2021-01-16
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
相关资源
最近更新 更多