我試圖從Deitel的書中做另一個練習。該程序計算每月的利息併爲每個儲戶打印新的餘額。由於練習是與動態記憶有關的章節的一部分,我使用「新」和「刪除」操作符。出於某種原因,我得到這兩個錯誤:C++ LNK1120和LNK2019錯誤:「未解析的外部符號WinMain @ 16」
LNK2019:解析外部符號的WinMain @函數___tmainCRTStartup引用16
致命錯誤LNK1120:1周無法解析的外部
下面是類的頭文件。
//SavingsAccount.h
//Header file for class SavingsAccount
class SavingsAccount
{
public:
static double annualInterestRate;
SavingsAccount(double amount=0);//default constructor intialize
//to 0 if no argument
double getBalance() const;//returns pointer to current balance
double calculateMonthlyInterest();
static void modifyInterestRate(double interestRate):
~SavingsAccount();//destructor
private:
double *savingsBalance;
};
與成員函數cpp文件定義
//SavingsAccount class defintion
#include "SavingsAccount.h"
double SavingsAccount::annualInterestRate=0;//define and intialize static data
//member at file scope
SavingsAccount::SavingsAccount(double amount)
:savingsBalance(new double(amount))//intialize savingsBalance to point to new object
{//empty body
}//end of constructor
double SavingsAccount::getBalance()const
{
return *savingsBalance;
}
double SavingsAccount::calculateMonthlyInterest()
{
double monthlyInterest=((*savingsBalance)*annualInterestRate)/12;
*savingsBalance=*savingsBalance+monthlyInterest;
return monthlyInterest;
}
void SavingsAccount::modifyInterestRate(double interestRate)
{
annualInterestRate=interestRate;
}
SavingsAccount::~SavingsAccount()
{
delete savingsBalance;
}//end of destructor
末最後的驅動程序:
#include <iostream>
#include "SavingsAccount.h"
using namespace std;
int main()
{
SavingsAccount saver1(2000.0);
SavingsAccount saver2(3000.0);
SavingsAccount::modifyInterestRate(0.03);//set interest rate to 3%
cout<<"Saver1 monthly interest: "<<saver1.calculateMonthlyInterest()<<endl;
cout<<"Saver2 monthly interest: "<<saver2.calculateMonthlyInterest()<<endl;
cout<<"Saver1 balance: "<<saver2.getBalance()<<endl;
cout<<"Saver1 balance: "<<saver2.getBalance()<<endl;
return 0;
}
我已經花了一個小時試圖弄清楚這一點同沒有成功。
就是這樣。謝謝!!! – Mike55 2009-08-09 19:48:16