从列表

时间:2016-06-12 09:47:27

标签: c++

所以BCD是另一个类BankCustomerDetails的对象列表。 BCD也是班级银行系统的数据成员 让我们说我想从列表中访问第二个客户的名字,如下所示: -
BCD [1]请将.Name
但这不起作用。

  class BankSystem
  {
    private:
        std::list<BankCustomerDetails>BCD;

    public:
        std::list<BankCustomerDetails>  GetBankCustomerDetails();
        void SetBankCustomerDetails(std::list<BankCustomerDetails>&);
        int GetTotalCustomerCount();
        void Create_AddCustomerAccount_BankSystem(BankCustomerDetails&);

        bool SearchCustomerDetails_byName(std::string);  
        bool SearchCustomerDetails_byAccountNumber(unsigned long int);

        void UpdateCustomerDetails_byAccountNumber(std::string ,unsigned long int );

        void DepositMoney_byAccountNumber(unsigned long int ,double);

        void WithdrawMoney_byAccountNumber(unsigned long int , double);

        double BalanceInquiryofCustomer_byAccountNumber(unsigned long int );

        void DeleteCustomerDetails_byAccountNumber(unsigned long int );

        void DisplayAllCustomerDetails();

   };



  class BankCustomerDetails
  {
    private:
        std::string Name;
        unsigned long int Account_Number;
        double Account_Balance;
    public:

        BankCustomerDetails();
        BankCustomerDetails(std::string, unsigned long in, double);

        std::string GetBankCustomerName(); 
        unsigned long int GetBankCustomerAccount_Number();
        double GetBankCustomerAccount_Balance();

        void SetBankCustomerName(std::string&); 
        void SetBankCustomerAccount_Number(unsigned long int&);
        void SetBankCustomerAccount_Balance(double&);

        void AcceptBankCustomerDetails();
        void DisplayBankCustomerDetails();
   };

这就是我想要做的事: - 在Client.cpp

string nm;
BankSystem BS;
cout<<"Enter The Customer Name to Search Details"<<endl;
cin>>nm;
int flag = BS.SearchCustomerDetails_byName(nm);

在BankSystem.cpp

bool BankSystem::SearchCustomerDetails_byName(std::string name)
{
    //Need to Implement
     for(int i = 0; i < BCD.size(); i++){
        if(BCDName = name)
           return true;
     }
return false;
}  

1 个答案:

答案 0 :(得分:0)

您无法使用索引和方括号访问std::list元素。我建议使用std::vector而不是std::list。然后你可以写:

for (int i = 0; i < BCD.size(); i++) {
    if (BCD[i].GetBankCustomerName() == name) {
       return true;
    }
}

当然你也可以使用list做同样的事情,但是你必须使用迭代器重写循环。

std::list<BankCustomerDetails>::iterator iterator;
for (iterator = intList.begin(); iterator != intList.end(); ++iterator)
    if (iterator->GetBankCustomerName() == name) {
       return true;
    }
}

我仍然建议使用vector因为它几乎总是有更好的性能。

还有两个注释:

  • 列表itemBCD[i].Name不起作用,因为Name是私有的。
  • 比较运算符为==而不是=