문제

I am creating a model of a bank system in C++, and there are multiple account types, all inheriting from the base class Account. I am unsure exactly what is causing the LNK errors, as I do not believe that I am using external libraries to the compiler, unless it is viewing my .h and .cpp files as external libraries. The error list that I am getting is:

1>------ Build started: Project: Bank, Configuration: Debug Win32 ------
1>  Account.cpp
1>CurrentAccount.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CurrentAccount::~CurrentAccount(void)" (??1CurrentAccount@@UAE@XZ) referenced in function "public: virtual void * __thiscall CurrentAccount::`scalar deleting destructor'(unsigned int)" (??_GCurrentAccount@@UAEPAXI@Z)
1>H:\C++ Assignment\Bank\Debug\Bank.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

And here is the Account.h file that all of the other classes inherit from

#pragma once
#include "Person.h"
using namespace std;

class Account
{
public: 
    enum AccountType {Current, JrCurrent, StdntSavings, CorpSavings, PersonalLoan, CorpLoan, Mortgage, GuaranteeCard, CreditCard, GFInvestment, EFInvestment, Pension};
    Account(double balance, double interestRate, Person accountHolder);
    Account(double balance, double interestRate, Person accountHolder, AccountType type);
    Account(){};
    virtual ~Account();
    double getBalance(), getInterestRate();
    Person getAccountHolder();
    void deposit(double amount), changeInterest(double newInterest), calculateInterest();
    bool isType(AccountType type);
    bool hasFunds();
    bool withdraw(double amount);
    string toString();

protected:
    Person accountHolder;
    double balance, interestRate, creditLimit;
    AccountType accType;
    friend ostream& operator<<(ostream &out, Account& other);
};

And as an example of how I am inheriting:

#pragma once
#include "Account.h"

class StudentSavings:public Account
{
    //stuff
};
도움이 되었습니까?

해결책

You didn't define your virtual destructor.
The error message clearly tells you that it is this symbol that is not defined.

virtual ~Account() {}

The linker works not just on third-party libraries, but on all definitions and symbols in your own code too. You must provide these when used (and a virtual destructor is always "used").

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top