好的,所以我目前是 C 的初級程式員(學校 11 年級,高中 3 年級,但我們仍在學習基本功能,所以請原諒任何新手錯誤)并且我正在自學額外的編碼知識。除了背景關系,我搬到了 Visual Studio 2022 并且我正在學習一門課程,目前正在學習繼承。
問題是,我有一個專案,其中有一個基本 Account 類和 3 個其他公共派生類(來自 Account 類),每個都有頭檔案和定義檔案,但是每當我嘗試運行 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\3. Inheritance - DerivingClasses.exe : fatal error LNK1120: 8 unresolved externals
從我收集到的資訊來看,編譯器要么很生氣,因為它沒有找到我在標題中宣告的函式的任何定義,要么很生氣它無法區分“存款”和“取款”函式每個類(每個類都有這兩個方法,并且它們在任何地方都命名相同,但是繼承應該可以正常作業,我已經檢查了代碼本身并且沒有出現其他編譯器錯誤)。在這一點上,我真的很難理解編譯器到底在哭什么以及如何解決它。
我試圖在所有檔案中包含所有標題,所有 .cpp 檔案無處不在。包含 .cpp 檔案只會讓它吐出 LNK1169(多重定義的符號),所以沒有 bueno。我已經嘗試將所有內容復制到一個新的解決方案中,但無濟于事,我已經兩次和三次檢查了所有代碼,我嘗試過移動檔案,我瀏覽了許多談論這種相同型別錯誤的帖子和他們似乎都沒有幫助。
(我專案檔案分發的截圖): https ://i.stack.imgur.com/gOXym.png
例如,這是我的基本 Account 類頭...
#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;
}
所有其他類都非常相似,因為繼承通常有效。
對于冗長的解釋感到抱歉,我對這一切還是陌生的,我只想確保我得到所有必要的細節。如果有人對這樣的幫助帖子有任何一般的禮儀提示,那也將不勝感激:)
提前致謝!
編輯:我將包含所有代碼 - 我認為這還不夠清楚。
Account.h 和 Account.cpp 包含在上面
儲蓄賬戶.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_
SavingsAccount.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;
}
CheckingAccount.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_
CheckingAccount.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;
}
信任帳戶.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_
信任帳戶.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 是我用來幫助顯示代碼和測驗它的部分。它只需要一個 Account(及其衍生品)物件和 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);
}
發布答案編輯:
這是更好的 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 } {}
uj5u.com熱心網友回復:
您對帳戶型別的 the和多載的deposit宣告withdraw。Acctools.hconststd::vector
修復方法是洗掉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_
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/428622.html
上一篇:我發現自己將代碼粘貼到子類中。除了繼承,我還能做些什么嗎?
下一篇:Kotlin-類繼承;覆寫屬性
