我不明白我做错了什么。我已经为其他项目做了很多次。
我已经链接了头文件,在补充cpp文件中使用了范围解析运算符,在main函数中创建了类对象,并且在我尝试调用某些内容时,我得到“未定义的引用或未定义的vtable”
我已经搜索了这个错误的解决方案,但它似乎过于笼统的错误代码,并且每个案例都不同,所以任何帮助或澄清我做错了什么都将非常感激。
这是错误
运行/home/ubuntu/workspace/main.cpp /tmp/cczZdJ5n.o:功能正常
main': /home/ubuntu/workspace/main.cpp:23: undefined reference to
BankAccount :: BankAccount()'/ home /ubuntu /workspace / main.cpp:36: 对SavingsAccount::withdraw(double)' /tmp/cczZdJ5n.o: In function
SavingsAccount :: SavingsAccount()'的未定义引用: /home/ubuntu/workspace/SavingsAccount.h:7:未定义引用BankAccount::BankAccount()' /home/ubuntu/workspace/SavingsAccount.h:7: undefined reference to
vtable for SavingsAccount'/ tmp / cczZdJ5n.o:在功能中CheckingAccount::CheckingAccount()': /home/ubuntu/workspace/CheckingAccount.h:7: undefined reference to
的BankAccount ::的BankAccount()” /home/ubuntu/workspace/CheckingAccount.h:7:未定义引用 `checktable的vtable'collect2:错误:ld返回1退出 状态
这就是所有相关代码:
main.cpp
#include <iostream>
#include "BankAccount.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include <fstream>
//#include <iomanip> format input and output
//#include <string>
//#include <ctime>
//#include <cmath>
//#include <cstdlib> random numbers, sort and search
using namespace std;
int funcTest()
{
return 777;
}
int main() {
BankAccount ba;
SavingsAccount sa;
CheckingAccount ca;
double amt;
cout << "Thank you for choosing \nComputing Plus Positive Bank\n";
cout << "Your balance is $" << ba.balance << endl;
cout <<"Savings\n";
do{
cout << "Enter an amount to withdraw and press Enter. Insert 0 to skip.\n";
cin >> amt;
if (amt > 0) {sa.withdraw(amt);}
}while(amt != 0);
cin.ignore();
cin.get();
}
BankAccount.h
#include <iostream>
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
using namespace std;
class BankAccount
{
public:
double balance;
int deposits;
int withdrawals;
double air;
double msc;
BankAccount();
double getBal();
int getDep();
int getWdw();
double getAir();
double getMsc();
virtual void deposit(double);
virtual void withdraw(double);
virtual void calcInt();
virtual void monthlyProc();
private:
protected:
};
#endif // BANKACCOUNT_H
BankAccount.cpp
#include "BankAccount.h"
BankAccount::BankAccount()
{
ifstream theFile("bankinfo.txt")
while(theFile >> balance >> air) {}
}
double BankAccount::getBal() {return balance;}
int BankAccount::getDep(){return deposits;}
int BankAccount::getWdw(){return withdrawals;}
double BankAccount::getAir(){return air;}
double BankAccount::getMsc(){return msc;}
virtual void BankAccount::deposit(double amtDep)
{
balance += amtDep;
deposits++;
}
virtual void BankAccount::withdraw(double amtWd)
{
balance -= amtWd;
withdrawals++;
}
virtual void BankAccount::calcInt()
{
double mir = air/12;
double mi = balance * mir;
balance =+ mi;
}
virtual void BankAccount::monthlyProc()
{
balance =- msc;
calcInt();
withdrawals = 0;
deposits = 0;
msc = 0;
}
SavingsAccount.h
#include <iostream>
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
using namespace std;
class SavingsAccount : public BankAccount
{
public:
bool active;
void status();
void withdraw();
void deposit();
void monthlyProc();
private:
protected:
};
#endif // SAVINGSACCOUNT_H
SavingsAccount.cpp
#include "SavingsAccount.h"
void SavingsAccount::status()
{
if (balance <25) {
Active = false;
}else {Active = true;}
}
void SavingsAccount::withdraw()
{
if(Active = true) {
BankAccount::withdraw();
}
status();
}
void SavingsAccount::deposit()
{
BankAccount::deposit();
status();
}
void SavingsAccount::monthlyProc()
{
if (withdrawals > 4) {
msc =+ withdrawals - 4;
}
BankAccount::monthlyProc();
status();
}
这些是我们提供的说明
Project #3 will be programming challenge #14 on page 969 of the text book. This is the Bank Accounts group project. Please be sure you follow the program requirements carefully and use inheritance and polymorphism as instructed.
Bank Accounts
Design a generic class to hold the following information about a bank account:
Balance
Number of deposits this month
Number of withdrawals
Annual interest rate
Monthly service charges
The class should have the following member functions:
Constructor: Accepts arguments for the balance and annual interest rate.
deposit: A virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should also increment the variable holding the number of deposits.
withdraw: A virtual function that accepts an argument for the amount of the withdrawal. The function should subtract the argument from the balance. It should also increment the variable holding the number of withdrawals.
calcInt: A virtual function that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance. This is performed by the following formulas:
Monthly Interest Rate = (Annual Interest Rate / 12)
Monthly Interest = Balance * Monthly Interest Rate
Balance = Balance + Monthly Interest
monthlyProc: A virtual function that subtracts the monthly service charges from the balance, calls the calcInt function, and then sets the variables that hold the number of withdrawals, number of deposits, and monthly service charges to zero.
Next, design a savings account class, derived from the generic account class. The sav-ings account class should have the following additional member:
status (to represent an active or inactive account)
If the balance of a savings account falls below $25, it becomes inactive. (The status member could be a flag variable.) No more withdrawals may be made until the bal-ance is raised above $25, at which time the account becomes active again. The savings account class should have the following member functions:
withdraw: A function that checks to see if the account is inactive before a with-drawal is made. (No withdrawal will be allowed if the account is not active.) A withdrawal is then made by calling the base class version of the function.
deposit: A function that checks to see if the account is inactive before a deposit is made. If the account is inactive and the deposit brings the balance above $25, the account becomes active again. The deposit is then made by calling the base class version of the function.
monthlyProc: Before the base class function is called, this function checks the num-ber of withdrawals. If the number of withdrawals for the month is more than 4, a service charge of $1 for each withdrawal above 4 is added to the base class variable that holds the monthly service charg-es. (Don’t forget to check the account balance after the service charge is taken. If the balance falls below $25, the account becomes inactive.)
Next, design a checking account class, also derived from the generic account class. It should have the following member functions:
withdraw: Before the base class function is called, this function will determine if a withdrawal (a check written) will cause the balance to go below $0. If the balance goes below $0, a service charge of $15 will be taken from the account. (The withdrawal will not be made.) If there isn’t enough in the account to pay the service charge, the balance will become nega-tive and the customer will owe the negative amount to the bank.
monthlyProc: Before the base class function is called, this function adds the monthly fee of $5 plus $0.10 per withdrawal (check written) to the base class variable that holds the monthly service charges.
Write a complete program that demonstrates these classes by asking the user to enter the amounts of deposits and withdrawals for a savings account and checking account. The program should display statistics for the month, including beginning balance, total amount of deposits, total amount of withdrawals, service charges, and ending balance. 970 Chapter 15 Inheritance, Polymorphism, and Virtual Functions
NOTE: You may need to add more member variables and functions to the classes than those listed above.