2009-08-09 474 views
5

我試圖從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; 
} 

我已經花了一個小時試圖弄清楚這一點同沒有成功。

回答

7

轉到「鏈接器設置 - >系統」。將「子系統」字段從「Windows」更改爲「控制檯」。

+0

就是這樣。謝謝!!! – Mike55 2009-08-09 19:48:16

2

創建新項目時,請選擇「Win32控制檯應用程序」而不是「Win32 Project」。

3

看起來您正在編寫標準控制檯應用程序(您有int main()),但鏈接程序期望找到一個Windows入口點WinMain

在yout項目的屬性頁中,在鏈接器部分的系統/子系統選項中,是否選擇了「Windows(/ SUBSYSTEM:WINDOWS)」?如果是,請嘗試將其更改爲「控制檯(/ SUBSYSTEM:CONSOLE)」

相關問題