【问题标题】:How to declare a Vector object in c++?如何在 C++ 中声明一个 Vector 对象?
【发布时间】:2016-01-29 22:59:01
【问题描述】:

我是学习 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;
}

Bank.h

#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 类。

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

但是这个错误一直出现在Bank.cpp的构造函数下:

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

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

【问题讨论】:

    标签: c++ vector


    【解决方案1】:

    问题在于没有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);
    }
    

    【讨论】:

      【解决方案2】:

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

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

      附带说明,您可能希望输入参数C_nameconst std::string&amp; cName(使用camelCase 保持一致,因此变量不应以大写字母开头,它仅用于类和结构)。

      通常,我们将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(),因此会报错。

      【讨论】:

      • 糟糕,我忘记粘贴了,谢谢你发现,但我仍然遇到同样的错误
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

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

        尝试稍后在对newAccount.push_back 的调用中给bk 一些参数,但为时已晚; 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。)

        【讨论】:

          【解决方案5】:

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

          但是当您声明标准构造函数时,您会遇到以下错误

          错误 1 ​​错误 LNK2019:未解析的外部符号“public: __thiscall BankAccount::BankAccount(void)”(??0BankAccount@@QAE@XZ) 在函数“public: __thiscall Bank::Bank(void)”(? ?0Bank@@QAE@XZ) [...]

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

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

          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 对象填充。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-22
            • 2013-05-20
            • 1970-01-01
            • 1970-01-01
            • 2017-12-18
            相关资源
            最近更新 更多