如何在 c++ 中声明 Vector 对象

How to declare a Vector object in c++?

本文关键字:声明 Vector 对象 c++      更新时间:2023-10-16

我是学习 C++ 的新手,我现在正处于创建一个包含类对象向量的类的阶段,其中包含添加新对象并将它们全部打印出来的方法。

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

银行账户.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

银行账户.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

银行.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));

}

银行账户类很好,主要问题在银行类中。

我创建了银行类来创建向量 银行账户 ,使用添加所有银行账户并将它们全部打印出来的方法。

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

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

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

问题是没有BankAccountsstd::vector。问题是您的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);
}

你只是忘记实现BankAccount类的构造函数。

BankAccount::BankAccount(string C_Name,int C_Balance)
    : customer_name(C_name)
    , customer_balance(C_Balance)
{
    // TODO;
}

作为旁注,您可能希望输入参数C_name是一个const std::string& cName(使用驼峰大小是一致的,因此变量不应以大写字母开头,它仅用于类和结构(。

通常,我们对私人成员使用variable_(您做了相反的事情(。另一个提示是,变量应该从构造函数的主体中初始化出来。

编辑:

// This is wrong
newAccount.push_back(bk(Customer_name,Customer_balance));

它应该是:

// Construct a valid BankAccount object using it's constructor to fill the vector
newAccount.push_back(BankAccount(Customer_name, Customer_balance));

变量bk是无用的,需要一个默认的构造函数BankAccount::BankAccount()因此会出现错误。

您尚未定义默认构造函数。当您使用push_back时,它将尝试默认构造对象,然后复制您正在传入的对象。您还默认构造 Bank 中的成员bk。您有 2 个选项可以解决此问题。

  1. 定义一个默认构造函数,即。 BankAccount() {
  2. 如果你使用的是 c++11,你可以在例程中使用向量emplace_back。您将需要重新设计以从 Bank 中删除bk成员。

类包含的所有对象都在构造函数的主体之前构造。在这种情况下,Bank对象直接包含一个BankAccount对象 bk ,该对象在 Bank 构造函数的开始{之前构造:

Bank :: Bank()
// ... bk is initialized here....
{

稍后尝试在调用 newAccount.push_backbk提供一些参数,但为时已晚;bk对象已初始化。

若要控制对象在构造函数中的初始化方式,必须在初始化列表中调用其构造函数:

Bank :: Bank()
  : bk(Customer_name,Customer_balance)
{

。但是当然,您还没有定义Customer_nameCustomer_balance.

因此,让我们备份一下:为什么要在初始化Bank时将帐户推送到Bank中?为什么此时甚至会存在帐户?为什么Bankbk作为会员?(当Bank也包含一整vector帐户时,为什么会特别对待该帐户?您以后将如何添加更多帐户?

所以,试试这个:首先,默认初始化Bank

Bank :: Bank() {}

。或者,在标题中(如果您使用的是 C++11 或 C++14,您应该这样做(:

Bank(void) =default;

然后,添加添加帐户的方法:

Bank::addAcount(void)
{
    // ... use stdin/stdout to get `customer_name` and `customer_balance`...
    newAccount.emplace_back(customer_name,customer_balance);
}

(请注意,如果您不使用 C++11 或 C++14,则需要添加push_back而不是emplace_back

似乎没有在您的 BankAccount.h 中定义BankAccount::BankAccount()标准构造函数。

但是当你声明标准构造函数时,你会在以下错误中运行

错误

1 错误 LNK2019:未解析的外部符号"公共:__thiscall 银行帐户::银行帐户(无效("(??0BankAccount@@QAE@XZ(在函数"public: __thiscall Bank::Bank(void("中引用(??0Bank@@QAE@XZ( [...]

表示成员变量Bank::bk已声明但从未定义。

因此,为了成功编译您的代码,我建议删除类Bank中成员变量bk的声明。这毫无意义。然后在银行类中执行以下操作。

Bank::Bank()
{
   string Customer_name = " ";
   int Customer_balance = 0;
   cout << "Add name please ";
   cin >> Customer_name;
   cout << "How much balance?";
   cin >> Customer_balance;
   BankAccount bankAccount(Customer_name, Customer_balance);
   newAccount.push_back(bankAccount);
}

您将成功验证向量newAccount填充新的 BankAccount 对象。