我正在制作一个银行帐户程序,无法弄清为什么我不断出现以下错误:
错误5错误LNK1120:1个未解决的外部
我有一个超类BankAccount和一个子类Checking Account。
银行帐户.h:
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class BankAccount
{
public:
BankAccount::BankAccount();
BankAccount::~BankAccount();
virtual void depsoit(double money) = 0;
virtual double withdraw(double money) = 0;
virtual double getBalance() = 0;
virtual void endOfMonth() = 0;
private:
double balance;
};
银行帐户.cpp
#include "BankAccount.h"
#include <iostream>
#include <vector>
using namespace std;
BankAccount::BankAccount()
{
balance = 0;
}
BankAccount::~BankAccount()
{
}
CheckingAccount.h
#pragma once
#include "BankAccount.h"
#include <vector>
#include <iostream>
using namespace std;
class CheckingAccount :
public BankAccount
{
public:
CheckingAccount();
~CheckingAccount();
void depsoit(double money);
double withdraw(double request);
double getBalance();
void endOfMonth();
private:
double checkingBalance=0;
int transactionLimit = 5;
float fee = .05;
double fees=0;
vector <double> feeTransactions;
};
CheckingAccount.cpp
#include "CheckingAccount.h"
#include <iostream>
#include <vector>
using namespace std;
CheckingAccount::CheckingAccount()
{
checkingBalance = 0;
}
CheckingAccount::~CheckingAccount()
{
}
void CheckingAccount::depsoit(double money)
{
if (transactionLimit > 0)
{
transactionLimit--;
cout << "You have " << transactionLimit << " transactions left";
checkingBalance += money;
}
else
{
feeTransactions.push_back(money);
cout << "Your transaction went through but you incurred a fee";
checkingBalance += money;
}
}
double CheckingAccount::withdraw(double request)
{
if (checkingBalance < request)
{
cout << "Sorry you do not have the available funds";
return 0.0;
}
else if (transactionLimit > 0)
{
transactionLimit--;
cout << "You have " << transactionLimit << " transactions left";
checkingBalance -= request;
return request;
}
else
{
feeTransactions.push_back(request);
cout << "Your transaction went through but you incurred a fee";
checkingBalance -= request;
return request;
}
}
double CheckingAccount::getBalance()
{
return checkingBalance;
}
void CheckingAccount::endOfMonth()
{
for (int i = 0; i < feeTransactions.size(); i++)
{
fees = feeTransactions[i] * fee;
}
checkingBalance -= fees;
}
最后是MAIN.CPP
#include "CheckingAccount.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
CheckingAccount test();
test().getBalance();
//system("PAUSE");
return 1;
}
错误消息再次是:
错误4错误LNK2019:未解析的外部符号“类
CheckingAccount __cdecl test(void)“(?test @@ YA?AVCheckingAccount @@ XZ)
在函数_main C:... Main.obj Assignment1Part3A中引用
最佳答案
您不是在这里声明CheckingAccount类型的变量:
CheckingAccount test();
您正在做的是声明一个函数测试,该测试返回类型为CheckingAccount的对象并且不接受任何参数。
如下进行:
CheckingAccount test{};
您应该按以下方式调用getBalance:
test.getBalance();
而不是test().getBalance();