如何解决外部符号

时间:2013-11-25 14:20:24

标签: c++ lnk2019

我正在用C ++创建一个银行系统模型,并且有多种帐户类型,都是从基类Account继承的。我不确定究竟是什么导致LNK错误,因为我不相信我正在使用外部库到编译器,除非它将我的.h和.cpp文件视为外部库。我得到的错误列表是:

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 ==========

这是所有其他类继承自

的Account.h文件
#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);
};

作为我如何继承的一个例子:

#pragma once
#include "Account.h"

class StudentSavings:public Account
{
    //stuff
};

1 个答案:

答案 0 :(得分:1)

您没有定义您的虚拟析构函数 错误消息清楚地告诉您,此符号未定义。

virtual ~Account() {}

链接器不仅适用于第三方库,还适用于您自己代码中的所有定义和符号。您必须在使用时提供这些(并且总是“使用”虚拟析构函数)。