LNK2019:未解析的外部符号 - 无法将 .h 文件与 Visual Studio 2022 中的 .cpp(定义)文件链接
LNK2019: unresolved external symbol - linking .h files with .cpp (definition) files in Visual Studio 2022 isn't working
好的,所以我目前是 C++ 的初级程序员(学校 11 年级,高中 3 年级,但我们仍在学习基本函数,所以请原谅任何菜鸟错误)并且我正在学习更多我自己的编码知识。除了上下文,我搬到了 Visual Studio 2022 年,我正在学习一门课程,目前正在学习继承。
事实是,我有一个项目有一个基本帐户 class 和其他 3 个公开派生的 classes(来自帐户 class)并且每个都有 headers 和定义文件,但每当我尝试 运行 main 时,我都会得到以下错误链:
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > const &,double)" (?deposit@@YAXAEBV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > const &,double)" (?withdraw@@YAXAEBV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > const &,double)" (?deposit@@YAXAEBV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > const &,double)" (?withdraw@@YAXAEBV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > const &,double)" (?deposit@@YAXAEBV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > const &,double)" (?withdraw@@YAXAEBV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > const &,double)" (?deposit@@YAXAEBV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > const &,double)" (?withdraw@@YAXAEBV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>C:\Users\Sapok\Desktop\Programming_projects\Visual Studio\ConstructorsAndDestructors\x64\Debug. Inheritance - DerivingClasses.exe : fatal error LNK1120: 8 unresolved externals
根据我收集到的信息,编译器要么因为没有找到我在 header 中声明的函数的任何定义而生气,要么因为无法区分两者而生气每个 class 的“存款”和“取款”函数(每个 class 都有这两个方法,它们在任何地方都被命名为相同的,但是继承应该可以正常工作,我检查了代码本身并且没有出现其他编译器错误)。在这一点上,我真的很困惑编译器到底在哭什么以及如何解决它。
我试图在所有文件中包含所有 header,所有 .cpp 文件无处不在。包括 .cpp 文件只会让它吐出 LNK1169(多重定义的符号),所以没有 bueno。我试过将所有内容复制到一个新的解决方案但无济于事,我已经对所有代码进行了两次和三次检查,我试过移动文件,我已经搜索了很多关于这种相同类型错误的帖子和none 其中似乎有所帮助。
(我的项目文件分发截图):
https://i.stack.imgur.com/gOXym.png
例如,这是我的基本帐户 class header...
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include <string>
#include <iostream>
class Account {
friend std::ostream& operator<< (std::ostream &out, const Account& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
protected:
std::string name;
double balance;
public:
Account(std::string name = def_name, double balance = def_balance);
bool deposit(double amount);
bool withdraw(double amount);
};
#endif // _ACCOUNT_H_
...及其定义.cpp 文件。
#include "Account.h"
#include <string>
#include <iostream>
Account::Account(std::string name, double balance)
: name{ name }, balance{ balance } {}
bool Account::deposit(double amount) {
if (amount < 0)
return false;
else {
balance += amount;
return true;
}
}
bool Account::withdraw(double amount) {
if (balance - amount < 0)
return false;
else {
balance -= amount;
return true;
}
}
std::ostream& operator<<(std::ostream& out, const Account& account) {
out << "[Account: " << account.name << ": " << account.balance << "]";
return out;
}
所有其他 class 非常相似,因为继承通常有效。
抱歉 long-winded 的解释,我对这一切还是陌生的,我只是想确保我了解所有必要的细节。如果有人对像这样的帮助帖子有任何一般礼仪提示,也将不胜感激:)
提前致谢!
编辑:我将包含所有代码 - 我认为这还不够清楚。
Account.h和Account.cpp包括在上面
储蓄Account.h:
#ifndef _SAVINGSACCOUNT_H_
#define _SAVINGSACCOUNT_H_
#include "Account.h"
#include <string>
#include <iostream>
class SavingsAccount : public Account{
friend std::ostream& operator<< (std::ostream& out, const SavingsAccount& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
static constexpr double def_interest = 0.0;
protected:
double interest;
public:
SavingsAccount(std::string name = def_name, double balance = def_balance, double interest = def_interest);
bool deposit(double amount);
// Inherited withdraw
};
#endif // _SAVINGSACCOUNT_H_
储蓄Account.cpp:
#include "SavingsAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
SavingsAccount::SavingsAccount(std::string name, double balance, double interest)
: Account(name, balance), interest{ interest } {}
bool SavingsAccount::deposit(double amount) {
amount += amount * (interest / 100);
return Account::deposit(amount);
}
std::ostream& operator<<(std::ostream& out, const SavingsAccount& account) {
out << "[SavingsAccount: " << account.name << ": " << account.balance << ", " << account.interest << "%]";
return out;
}
正在检查Account.h:
#ifndef _CHECKINGACCOUNT_H_
#define _CHECKINGACCOUNT_H_
#include "Account.h"
#include <string>
#include <iostream>
class CheckingAccount : public Account {
friend std::ostream& operator<< (std::ostream& out, const CheckingAccount& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
static constexpr double def_fee = 1.5;
public:
CheckingAccount(std::string name = def_name, double balance = def_balance);
// Inherited deposit
bool withdraw(double amount);
};
#endif // _CHECKINGACCOUNT_H_
正在检查Account.cpp:
#include "CheckingAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
CheckingAccount::CheckingAccount(std::string name, double balance)
: Account(name, balance) {}
bool CheckingAccount::withdraw(double amount) {
amount += def_fee;
return Account::withdraw(amount);
}
std::ostream& operator<<(std::ostream& out, const CheckingAccount& account) {
out << "[CheckingAccount: " << account.name << ": " << account.balance << ", " << account.def_fee << " withdrawal fee]";
return out;
}
信任Account.h:
#ifndef _TRUSTACCOUNT_H_
#define _TRUSTACCOUNT_H_
#include "SavingsAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
class TrustAccount : public SavingsAccount {
friend std::ostream& operator<< (std::ostream& out, const TrustAccount& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
static constexpr double def_interest = 0.0;
static constexpr double def_bonus = 50.0;
static constexpr double def_minimum = 0.2;
protected:
int limit;
public:
TrustAccount(std::string name = def_name, double balance = def_balance, double interest = def_interest, int limit = 0);
bool deposit(double amount);
bool withdraw(double amount);
};
#endif // _TRUSTACCOUNT_H_
信任Account.cpp:
#include "TrustAccount.h"
#include "SavingsAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
TrustAccount::TrustAccount(std::string name, double balance, double interest, int limit)
: SavingsAccount(name, balance, interest), limit{ 0 } {}
bool TrustAccount::deposit(double amount) {
if (!SavingsAccount::deposit(amount))
return false;
if (amount >= 5000)
Account::deposit(def_bonus);
return true;
}
bool TrustAccount::withdraw(double amount) {
if ((limit >= 3) || (amount > (balance * def_minimum)))
return false;
limit++;
return SavingsAccount::withdraw(amount);
}
std::ostream& operator<<(std::ostream& out, const TrustAccount& account) {
out << "[TrustAccount: " << account.name << ": " << account.balance << ", " << account.interest << "%, " << account.def_bonus << " deposit bonus, " << (3 - account.limit) << " withdrawals remaining]";
return out;
}
Acctools 是我用来帮助显示代码和测试代码的部分。它只需要一个帐户向量(及其衍生物)objects 和 mass-deposits/mass-withdraws/displays-all.
Acctools.h:
#ifndef _ACCTOOLS_H_
#define _ACCTOOLS_H_
#include <vector>
#include <string>
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
// Accounts
void display(const std::vector<Account>& accounts);
void deposit(const std::vector<Account>& accounts, double balance);
void withdraw(const std::vector<Account>& accounts, double balance);
// SavingsAccounts
void display(const std::vector<SavingsAccount>& accounts);
void deposit(const std::vector<SavingsAccount>& accounts, double balance);
void withdraw(const std::vector<SavingsAccount>& accounts, double balance);
// CheckingAccounts
void display(const std::vector<CheckingAccount>& accounts);
void deposit(const std::vector<CheckingAccount>& accounts, double balance);
void withdraw(const std::vector<CheckingAccount>& accounts, double balance);
// TrustAccounts
void display(const std::vector<TrustAccount>& accounts);
void deposit(const std::vector<TrustAccount>& accounts, double balance);
void withdraw(const std::vector<TrustAccount>& accounts, double balance);
#endif // _ACCTOOLS_H_
Acctools.cpp:
#include "Acctools.h"
#include <vector>
#include <string>
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
// Accounts
void display(const std::vector<Account>& accounts) {
std::cout << "\n===Accounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<Account>& accounts, double amount) {
std::cout << "\n===Depositing to Accounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<Account>& accounts, double amount) {
std::cout << "\n===Withdrawing from Accounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
// Savings Accounts
void display(const std::vector<SavingsAccount>& accounts) {
std::cout << "\n===SavingsAccounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<SavingsAccount>& accounts, double amount) {
std::cout << "\n===Depositing to SavingsAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<SavingsAccount>& accounts, double amount) {
std::cout << "\n===Withdrawing from SavingsAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
// Checking Accounts
void display(const std::vector<CheckingAccount>& accounts) {
std::cout << "\n===CheckingAccounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<CheckingAccount>& accounts, double amount) {
std::cout << "\n===Depositing to CheckingAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<CheckingAccount>& accounts, double amount) {
std::cout << "\n===Withdrawing from CheckingAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
// Trust Accounts
void display(const std::vector<TrustAccount>& accounts) {
std::cout << "\n===TrustAccounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<TrustAccount>& accounts, double amount) {
std::cout << "\n===Depositing to TrustAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<TrustAccount>& accounts, double amount) {
std::cout << "\n===Withdrawing from TrustAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
最后,主要的:
#include <iostream>
#include <string>
#include <vector>
#include "Acctools.h"
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
int main() {
//////////////////////////////////////////////////// ACCOUNTS
std::vector<Account> accounts;
accounts.push_back(Account{ "Cata", 2000 });
accounts.push_back(Account{ "Pernea", 0 });
accounts.push_back(Account{ "Razvi", 4000 });
display(accounts);
deposit(accounts, 1000);
display(accounts);
withdraw(accounts, 3000);
display(accounts);
//////////////////////////////////////////////////// SAVINGS ACCOUNTS
std::vector<SavingsAccount> savingsaccounts;
savingsaccounts.push_back(SavingsAccount{ "Sapokee", 2000, 5.0 });
savingsaccounts.push_back(SavingsAccount{ "Bernea", 0, 200.0 });
savingsaccounts.push_back(SavingsAccount{ "Ravi", 4000, 1.0 });
display(savingsaccounts);
deposit(savingsaccounts, 1000);
display(savingsaccounts);
withdraw(savingsaccounts, 3000);
display(savingsaccounts);
//////////////////////////////////////////////////// CHECKING ACCOUNTS
std::vector<CheckingAccount> checkingaccounts;
checkingaccounts.push_back(CheckingAccount{ "Sapok", 2000 });
checkingaccounts.push_back(CheckingAccount{ "Alex Bernea", 0 });
checkingaccounts.push_back(CheckingAccount{ "Ravioli", 4000});
display(checkingaccounts);
deposit(checkingaccounts, 1000);
display(checkingaccounts);
withdraw(checkingaccounts, 3000);
display(checkingaccounts);
//////////////////////////////////////////////////// TRUST ACCOUNTS
std::vector<TrustAccount> trustaccounts;
trustaccounts.push_back(TrustAccount{ "Sapokee", 2000, 5.0 });
trustaccounts.push_back(TrustAccount{ "Bernea", 0, 1.0 });
trustaccounts.push_back(TrustAccount{ "Ravi", 4000, 1.0 });
display(trustaccounts);
deposit(trustaccounts, 5000);
display(trustaccounts);
withdraw(trustaccounts, 3000);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
}
Post 答案编辑:
下面是更好的 TrustAccount 初始化:
private:
// ... all other default values
static constexpr int def_withdrawals = 0;
protected:
int withdrawals; // changed the name to be more intuitive
public:
TrustAccount(std::string name = def_name, double balance = def_balance, double interest = def_interest); // removed withdrawals from here
TrustAccount::TrustAccount(std::string name, double balance, double interest)
: SavingsAccount(name, balance, interest), withdrawals{ def_withdrawals } {}
您在 Acctools.h
中对 deposit
和 withdraw
重载的声明在帐户类型的 std::vector
上有 const
。
解决方法是删除 const
,以便声明与 Acctools.cpp
:
中的定义匹配
#ifndef _ACCTOOLS_H_
#define _ACCTOOLS_H_
#include <vector>
#include <string>
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
// Accounts
void display(const std::vector<Account>& accounts);
void deposit(std::vector<Account>& accounts, double balance);
void withdraw(std::vector<Account>& accounts, double balance);
// SavingsAccounts
void display(const std::vector<SavingsAccount>& accounts);
void deposit(std::vector<SavingsAccount>& accounts, double balance);
void withdraw(std::vector<SavingsAccount>& accounts, double balance);
// CheckingAccounts
void display(const std::vector<CheckingAccount>& accounts);
void deposit(std::vector<CheckingAccount>& accounts, double balance);
void withdraw(std::vector<CheckingAccount>& accounts, double balance);
// TrustAccounts
void display(const std::vector<TrustAccount>& accounts);
void deposit(std::vector<TrustAccount>& accounts, double balance);
void withdraw(std::vector<TrustAccount>& accounts, double balance);
#endif // _ACCTOOLS_H_
好的,所以我目前是 C++ 的初级程序员(学校 11 年级,高中 3 年级,但我们仍在学习基本函数,所以请原谅任何菜鸟错误)并且我正在学习更多我自己的编码知识。除了上下文,我搬到了 Visual Studio 2022 年,我正在学习一门课程,目前正在学习继承。
事实是,我有一个项目有一个基本帐户 class 和其他 3 个公开派生的 classes(来自帐户 class)并且每个都有 headers 和定义文件,但每当我尝试 运行 main 时,我都会得到以下错误链:
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > const &,double)" (?deposit@@YAXAEBV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > const &,double)" (?withdraw@@YAXAEBV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > const &,double)" (?deposit@@YAXAEBV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > const &,double)" (?withdraw@@YAXAEBV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > const &,double)" (?deposit@@YAXAEBV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > const &,double)" (?withdraw@@YAXAEBV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > const &,double)" (?deposit@@YAXAEBV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl deposit(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?deposit@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl deposit(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?deposit@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::deposit(double)" (?deposit@Account@@QEAA_NN@Z)
1> "public: bool __cdecl SavingsAccount::deposit(double)" (?deposit@SavingsAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::deposit(double)" (?deposit@TrustAccount@@QEAA_NN@Z)
1>SectionChallenge.obj : error LNK2019: unresolved external symbol "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > const &,double)" (?withdraw@@YAXAEBV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z) referenced in function main
1> Hint on symbols that are defined and could potentially match:
1> "void __cdecl withdraw(class std::vector<class Account,class std::allocator<class Account> > &,double)" (?withdraw@@YAXAEAV?$vector@VAccount@@V?$allocator@VAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class CheckingAccount,class std::allocator<class CheckingAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VCheckingAccount@@V?$allocator@VCheckingAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class SavingsAccount,class std::allocator<class SavingsAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VSavingsAccount@@V?$allocator@VSavingsAccount@@@std@@@std@@N@Z)
1> "void __cdecl withdraw(class std::vector<class TrustAccount,class std::allocator<class TrustAccount> > &,double)" (?withdraw@@YAXAEAV?$vector@VTrustAccount@@V?$allocator@VTrustAccount@@@std@@@std@@N@Z)
1> "public: bool __cdecl Account::withdraw(double)" (?withdraw@Account@@QEAA_NN@Z)
1> "public: bool __cdecl CheckingAccount::withdraw(double)" (?withdraw@CheckingAccount@@QEAA_NN@Z)
1> "public: bool __cdecl TrustAccount::withdraw(double)" (?withdraw@TrustAccount@@QEAA_NN@Z)
1>C:\Users\Sapok\Desktop\Programming_projects\Visual Studio\ConstructorsAndDestructors\x64\Debug. Inheritance - DerivingClasses.exe : fatal error LNK1120: 8 unresolved externals
根据我收集到的信息,编译器要么因为没有找到我在 header 中声明的函数的任何定义而生气,要么因为无法区分两者而生气每个 class 的“存款”和“取款”函数(每个 class 都有这两个方法,它们在任何地方都被命名为相同的,但是继承应该可以正常工作,我检查了代码本身并且没有出现其他编译器错误)。在这一点上,我真的很困惑编译器到底在哭什么以及如何解决它。
我试图在所有文件中包含所有 header,所有 .cpp 文件无处不在。包括 .cpp 文件只会让它吐出 LNK1169(多重定义的符号),所以没有 bueno。我试过将所有内容复制到一个新的解决方案但无济于事,我已经对所有代码进行了两次和三次检查,我试过移动文件,我已经搜索了很多关于这种相同类型错误的帖子和none 其中似乎有所帮助。
(我的项目文件分发截图): https://i.stack.imgur.com/gOXym.png
例如,这是我的基本帐户 class header...
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include <string>
#include <iostream>
class Account {
friend std::ostream& operator<< (std::ostream &out, const Account& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
protected:
std::string name;
double balance;
public:
Account(std::string name = def_name, double balance = def_balance);
bool deposit(double amount);
bool withdraw(double amount);
};
#endif // _ACCOUNT_H_
...及其定义.cpp 文件。
#include "Account.h"
#include <string>
#include <iostream>
Account::Account(std::string name, double balance)
: name{ name }, balance{ balance } {}
bool Account::deposit(double amount) {
if (amount < 0)
return false;
else {
balance += amount;
return true;
}
}
bool Account::withdraw(double amount) {
if (balance - amount < 0)
return false;
else {
balance -= amount;
return true;
}
}
std::ostream& operator<<(std::ostream& out, const Account& account) {
out << "[Account: " << account.name << ": " << account.balance << "]";
return out;
}
所有其他 class 非常相似,因为继承通常有效。
抱歉 long-winded 的解释,我对这一切还是陌生的,我只是想确保我了解所有必要的细节。如果有人对像这样的帮助帖子有任何一般礼仪提示,也将不胜感激:)
提前致谢!
编辑:我将包含所有代码 - 我认为这还不够清楚。
Account.h和Account.cpp包括在上面
储蓄Account.h:
#ifndef _SAVINGSACCOUNT_H_
#define _SAVINGSACCOUNT_H_
#include "Account.h"
#include <string>
#include <iostream>
class SavingsAccount : public Account{
friend std::ostream& operator<< (std::ostream& out, const SavingsAccount& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
static constexpr double def_interest = 0.0;
protected:
double interest;
public:
SavingsAccount(std::string name = def_name, double balance = def_balance, double interest = def_interest);
bool deposit(double amount);
// Inherited withdraw
};
#endif // _SAVINGSACCOUNT_H_
储蓄Account.cpp:
#include "SavingsAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
SavingsAccount::SavingsAccount(std::string name, double balance, double interest)
: Account(name, balance), interest{ interest } {}
bool SavingsAccount::deposit(double amount) {
amount += amount * (interest / 100);
return Account::deposit(amount);
}
std::ostream& operator<<(std::ostream& out, const SavingsAccount& account) {
out << "[SavingsAccount: " << account.name << ": " << account.balance << ", " << account.interest << "%]";
return out;
}
正在检查Account.h:
#ifndef _CHECKINGACCOUNT_H_
#define _CHECKINGACCOUNT_H_
#include "Account.h"
#include <string>
#include <iostream>
class CheckingAccount : public Account {
friend std::ostream& operator<< (std::ostream& out, const CheckingAccount& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
static constexpr double def_fee = 1.5;
public:
CheckingAccount(std::string name = def_name, double balance = def_balance);
// Inherited deposit
bool withdraw(double amount);
};
#endif // _CHECKINGACCOUNT_H_
正在检查Account.cpp:
#include "CheckingAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
CheckingAccount::CheckingAccount(std::string name, double balance)
: Account(name, balance) {}
bool CheckingAccount::withdraw(double amount) {
amount += def_fee;
return Account::withdraw(amount);
}
std::ostream& operator<<(std::ostream& out, const CheckingAccount& account) {
out << "[CheckingAccount: " << account.name << ": " << account.balance << ", " << account.def_fee << " withdrawal fee]";
return out;
}
信任Account.h:
#ifndef _TRUSTACCOUNT_H_
#define _TRUSTACCOUNT_H_
#include "SavingsAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
class TrustAccount : public SavingsAccount {
friend std::ostream& operator<< (std::ostream& out, const TrustAccount& account);
private:
static constexpr const char* def_name = "Unnamed account";
static constexpr double def_balance = 0.0;
static constexpr double def_interest = 0.0;
static constexpr double def_bonus = 50.0;
static constexpr double def_minimum = 0.2;
protected:
int limit;
public:
TrustAccount(std::string name = def_name, double balance = def_balance, double interest = def_interest, int limit = 0);
bool deposit(double amount);
bool withdraw(double amount);
};
#endif // _TRUSTACCOUNT_H_
信任Account.cpp:
#include "TrustAccount.h"
#include "SavingsAccount.h"
#include "Account.h"
#include <string>
#include <iostream>
TrustAccount::TrustAccount(std::string name, double balance, double interest, int limit)
: SavingsAccount(name, balance, interest), limit{ 0 } {}
bool TrustAccount::deposit(double amount) {
if (!SavingsAccount::deposit(amount))
return false;
if (amount >= 5000)
Account::deposit(def_bonus);
return true;
}
bool TrustAccount::withdraw(double amount) {
if ((limit >= 3) || (amount > (balance * def_minimum)))
return false;
limit++;
return SavingsAccount::withdraw(amount);
}
std::ostream& operator<<(std::ostream& out, const TrustAccount& account) {
out << "[TrustAccount: " << account.name << ": " << account.balance << ", " << account.interest << "%, " << account.def_bonus << " deposit bonus, " << (3 - account.limit) << " withdrawals remaining]";
return out;
}
Acctools 是我用来帮助显示代码和测试代码的部分。它只需要一个帐户向量(及其衍生物)objects 和 mass-deposits/mass-withdraws/displays-all.
Acctools.h:
#ifndef _ACCTOOLS_H_
#define _ACCTOOLS_H_
#include <vector>
#include <string>
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
// Accounts
void display(const std::vector<Account>& accounts);
void deposit(const std::vector<Account>& accounts, double balance);
void withdraw(const std::vector<Account>& accounts, double balance);
// SavingsAccounts
void display(const std::vector<SavingsAccount>& accounts);
void deposit(const std::vector<SavingsAccount>& accounts, double balance);
void withdraw(const std::vector<SavingsAccount>& accounts, double balance);
// CheckingAccounts
void display(const std::vector<CheckingAccount>& accounts);
void deposit(const std::vector<CheckingAccount>& accounts, double balance);
void withdraw(const std::vector<CheckingAccount>& accounts, double balance);
// TrustAccounts
void display(const std::vector<TrustAccount>& accounts);
void deposit(const std::vector<TrustAccount>& accounts, double balance);
void withdraw(const std::vector<TrustAccount>& accounts, double balance);
#endif // _ACCTOOLS_H_
Acctools.cpp:
#include "Acctools.h"
#include <vector>
#include <string>
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
// Accounts
void display(const std::vector<Account>& accounts) {
std::cout << "\n===Accounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<Account>& accounts, double amount) {
std::cout << "\n===Depositing to Accounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<Account>& accounts, double amount) {
std::cout << "\n===Withdrawing from Accounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
// Savings Accounts
void display(const std::vector<SavingsAccount>& accounts) {
std::cout << "\n===SavingsAccounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<SavingsAccount>& accounts, double amount) {
std::cout << "\n===Depositing to SavingsAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<SavingsAccount>& accounts, double amount) {
std::cout << "\n===Withdrawing from SavingsAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
// Checking Accounts
void display(const std::vector<CheckingAccount>& accounts) {
std::cout << "\n===CheckingAccounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<CheckingAccount>& accounts, double amount) {
std::cout << "\n===Depositing to CheckingAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<CheckingAccount>& accounts, double amount) {
std::cout << "\n===Withdrawing from CheckingAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
// Trust Accounts
void display(const std::vector<TrustAccount>& accounts) {
std::cout << "\n===TrustAccounts========================================" << '\n';
for (const auto& acc : accounts)
std::cout << acc << '\n';
}
void deposit(std::vector<TrustAccount>& accounts, double amount) {
std::cout << "\n===Depositing to TrustAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.deposit(amount))
std::cout << "Deposited " << amount << " to " << acc << '\n';
else
std::cout << "Failed deposit of " << amount << " to " << acc << '\n';
}
void withdraw(std::vector<TrustAccount>& accounts, double amount) {
std::cout << "\n===Withdrawing from TrustAccounts========================================" << '\n';
for (auto& acc : accounts)
if (acc.withdraw(amount))
std::cout << "Withdrawn " << amount << " from " << acc << '\n';
else
std::cout << "Failed withdrawal of " << amount << " from " << acc << '\n';
}
最后,主要的:
#include <iostream>
#include <string>
#include <vector>
#include "Acctools.h"
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
int main() {
//////////////////////////////////////////////////// ACCOUNTS
std::vector<Account> accounts;
accounts.push_back(Account{ "Cata", 2000 });
accounts.push_back(Account{ "Pernea", 0 });
accounts.push_back(Account{ "Razvi", 4000 });
display(accounts);
deposit(accounts, 1000);
display(accounts);
withdraw(accounts, 3000);
display(accounts);
//////////////////////////////////////////////////// SAVINGS ACCOUNTS
std::vector<SavingsAccount> savingsaccounts;
savingsaccounts.push_back(SavingsAccount{ "Sapokee", 2000, 5.0 });
savingsaccounts.push_back(SavingsAccount{ "Bernea", 0, 200.0 });
savingsaccounts.push_back(SavingsAccount{ "Ravi", 4000, 1.0 });
display(savingsaccounts);
deposit(savingsaccounts, 1000);
display(savingsaccounts);
withdraw(savingsaccounts, 3000);
display(savingsaccounts);
//////////////////////////////////////////////////// CHECKING ACCOUNTS
std::vector<CheckingAccount> checkingaccounts;
checkingaccounts.push_back(CheckingAccount{ "Sapok", 2000 });
checkingaccounts.push_back(CheckingAccount{ "Alex Bernea", 0 });
checkingaccounts.push_back(CheckingAccount{ "Ravioli", 4000});
display(checkingaccounts);
deposit(checkingaccounts, 1000);
display(checkingaccounts);
withdraw(checkingaccounts, 3000);
display(checkingaccounts);
//////////////////////////////////////////////////// TRUST ACCOUNTS
std::vector<TrustAccount> trustaccounts;
trustaccounts.push_back(TrustAccount{ "Sapokee", 2000, 5.0 });
trustaccounts.push_back(TrustAccount{ "Bernea", 0, 1.0 });
trustaccounts.push_back(TrustAccount{ "Ravi", 4000, 1.0 });
display(trustaccounts);
deposit(trustaccounts, 5000);
display(trustaccounts);
withdraw(trustaccounts, 3000);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
withdraw(trustaccounts, 100);
display(trustaccounts);
}
Post 答案编辑:
下面是更好的 TrustAccount 初始化:
private:
// ... all other default values
static constexpr int def_withdrawals = 0;
protected:
int withdrawals; // changed the name to be more intuitive
public:
TrustAccount(std::string name = def_name, double balance = def_balance, double interest = def_interest); // removed withdrawals from here
TrustAccount::TrustAccount(std::string name, double balance, double interest)
: SavingsAccount(name, balance, interest), withdrawals{ def_withdrawals } {}
您在 Acctools.h
中对 deposit
和 withdraw
重载的声明在帐户类型的 std::vector
上有 const
。
解决方法是删除 const
,以便声明与 Acctools.cpp
:
#ifndef _ACCTOOLS_H_
#define _ACCTOOLS_H_
#include <vector>
#include <string>
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include "TrustAccount.h"
// Accounts
void display(const std::vector<Account>& accounts);
void deposit(std::vector<Account>& accounts, double balance);
void withdraw(std::vector<Account>& accounts, double balance);
// SavingsAccounts
void display(const std::vector<SavingsAccount>& accounts);
void deposit(std::vector<SavingsAccount>& accounts, double balance);
void withdraw(std::vector<SavingsAccount>& accounts, double balance);
// CheckingAccounts
void display(const std::vector<CheckingAccount>& accounts);
void deposit(std::vector<CheckingAccount>& accounts, double balance);
void withdraw(std::vector<CheckingAccount>& accounts, double balance);
// TrustAccounts
void display(const std::vector<TrustAccount>& accounts);
void deposit(std::vector<TrustAccount>& accounts, double balance);
void withdraw(std::vector<TrustAccount>& accounts, double balance);
#endif // _ACCTOOLS_H_