我是学习c ++的新手,并且现在处于创建包含类对象向量的类的阶段,该类具有添加新对象并将其全部打印出来的方法。

到目前为止,这是我的代码:

BankAccount.h:

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using namespace std;

class BankAccount
{
    public:
        BankAccount(string C_Name,int C_Balance);
        /*
        void SetCustomerName(string C_Name);
        String GetCustomerName();
        void SetCustomerBalance(int C_Balance);
        int GetCustomerBalance();
        */
        int deposit(int deposit_);
        int withdraw(int withdraw_);
    private:
        string customer_name;
        int customer_balance = 0;
        int Deposit = 0;
        int Withdraw = 0;

};

#endif // BANKACCOUNT_H


BankAccount.cpp:

BankAccount::BankAccount(string C_Name,int C_Balance)
{
    customer_name = C_Name;
    customer_balance = C_Balance;
}


int BankAccount :: deposit(int deposit_){

        Deposit = deposit_;
        Deposit = Deposit + customer_balance;
        cout << "\nDeposit Balance = " << Deposit;
        customer_balance = Deposit;
        return customer_balance;

    }
int BankAccount :: withdraw(int withdraw_){
        Withdraw = withdraw_;
        Withdraw = customer_balance - Withdraw;
        customer_balance = Withdraw;
        cout<<"After Withdraw Balance is "<<customer_balance;
        return customer_balance;
}


银行

#ifndef BANK_H
#define BANK_H
#include <vector>
#include "BankAccount.h"
using namespace std;


class Bank
{
    public:
        //variables , lists
        vector<BankAccount> newAccount;
        BankAccount bk;

        // constructor
        Bank();

};

#endif // BANK_H


Bank.cpp:

#include "Bank.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

Bank :: Bank()
{
    string Customer_name = " ";
    int Customer_balance = 0;

    cout << "Add name please ";
    cin >> Customer_name ;

    cout << "How much balance?";
    cin >> Customer_balance;

 newAccount.push_back(bk(Customer_name,Customer_balance));


}


BankAccount类很好,主要问题在Bank类中。

我已经创建了bank类来创建BankAccount的向量,其方法将添加所有BankAccount并全部打印出来。

但是,此错误始终出现在Bank.cpp的构造函数下:

error: no matching function for call to 'BankAccount::BankAccount()'


似乎每当我试图在BankAccount向量中声明类对象时,错误就不断发生。有人可以解释我在做什么错以及如何解决这个问题吗?

最佳答案

问题不是std::vectorBankAccounts。问题是您的Bank类具有定义的数据成员:BankAccount bk;由于您没有任何显式的构造函数参数,因此它将尝试使用默认的构造函数BankAccount()。没有声明构造函数,因此会出现编译错误。

我怀疑您实际上并不需要该bk数据成员,应该将其删除。

下一个问题是当您尝试push_back时,您正在调用bk对象而不是构造对象。你想要的就是拥有

newAccount.push_back(BankAccount(Customer_name,Customer_balance));


如果您使用的是C++11或更高版本(看起来像您一样),则可以使用emplace_back

newAccount.emplace_back(Customer_name,Customer_balance);


这将使您的银行帐户类别如下:

class Bank {
  public:
    std::vector<BankAccount> newAccount;
    Bank();
};

Bank::Bank() {
    std::string Customer_name = " ";
    int Customer_balance = 0;

    std::cout << "Add name please ";
    std::cin >> Customer_name ;

    std::cout << "How much balance?";
    std::cin >> Customer_balance;

   newAccount.emplace_back(Customer_name,Customer_balance);
}

关于c++ - 如何在C++中声明Vector对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35095621/

10-09 03:59