简体   繁体   中英

Unresolved Externals, Unresolved external symbol LNK 1120

I am making a bank account program and can not figure out why I keep getting the following error:

Error 5 error LNK1120: 1 unresolved externals

I have a superclass BankAccount and a child class Checking Account.

Bank 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;

};

Bank account .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;
}

And finally MAIN.CPP

#include "CheckingAccount.h"
#include <iostream>
#include <vector>

using namespace std;

int main()
{

    CheckingAccount test();
    test().getBalance();

    //system("PAUSE");
    return 1;
}

The error message again is:

Error 4 error LNK2019: unresolved external symbol "class CheckingAccount __cdecl test(void)" (?test@@YA?AVCheckingAccount@@XZ) referenced in function _main C:...Main.obj Assignment1Part3A

You are not declaring a variable of type CheckingAccount here:

CheckingAccount test();

What you are doing is declaring a function test which returns an object of type CheckingAccount and takes in no parameters.

Do it as follows:

CheckingAccount test{};

You should call getBalance as follows:

test.getBalance(); and not test().getBalance();

Well the error tells you exactly what's the problem. It's in your main: test().getBalance(); . You are calling test as it was a function, but it's an object name. It should be just test.getBalance() . The previous line should be without the () too, but they should be fine there, because the compiler is treating them as a constructor in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM