如何在 c++ 中声明 Vector 对象
How to declare a Vector object in c++?
我是学习 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 向量中声明类对象时,错误就会不断发生。有人可以解释一下我做错了什么以及如何解决这个问题吗?
问题是没有BankAccounts
的std::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 个选项可以解决此问题。
- 定义一个默认构造函数,即。
BankAccount() {
- 如果你使用的是 c++11,你可以在例程中使用向量
emplace_back
。您将需要重新设计以从 Bank 中删除bk
成员。
类包含的所有对象都在构造函数的主体之前构造。在这种情况下,Bank
对象直接包含一个BankAccount
对象 bk
,该对象在 Bank
构造函数的开始{
之前构造:
Bank :: Bank()
// ... bk is initialized here....
{
您稍后尝试在调用 newAccount.push_back
时bk
提供一些参数,但为时已晚;bk
对象已初始化。
若要控制对象在构造函数中的初始化方式,必须在初始化列表中调用其构造函数:
Bank :: Bank()
: bk(Customer_name,Customer_balance)
{
。但是当然,您还没有定义Customer_name
或Customer_balance
.
因此,让我们备份一下:为什么要在初始化Bank
时将帐户推送到Bank
中?为什么此时甚至会存在帐户?为什么Bank
有bk
作为会员?(当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 对象。
- 向量索引变量声明(size_t 或 std::vector<DATATYPE>::size_type)
- “无法将函数定义与现有声明匹配”,当包装在 std::vector 中时
- 如何在多个文件中声明全局 std::vector 2d 数组?C++
- "Vector subscript out of range",在返回声明?
- 分别声明和初始化 vector<vector<int>>
- "vector"未在此范围内声明
- 指向STD :: vector,指针声明
- 使用 vector 在 c++ 中声明 3D 数组结构
- 声明全局变量 <vector>,代码将无法成功运行
- 为什么在使用#define声明int的vector的vector时没有抛出错误
- 我应该将我的 Vector<> 声明为 Vector<>* 吗?
- 如何用'n'维数组声明 std::vector?
- EXC_BAD_ACCESS 在 std::vector <double>的静态声明意外破坏时
- 声明"vector<int> myvector()"做什么?(关于无参数构造函数的混淆)
- 声明具有特定大小的 vector<vector<pair<int、int>>?
- 为什么我可以在std::vector中使用正向声明类
- 如何在 c++ 中声明 Vector 对象
- 在c++中声明vector为全局变量
- 在for循环中声明vector操作
- 错误:'vector'未命名类型/错误:'<'令牌之前的预期','或'...'/错误:尚未声明'vector'