C++ LNK1120和LNK2019错误:"未解析的外部符号WinMain@16"

7
我正在尝试完成Deitel的书中的另一个练习。该程序计算每个储户的月利息并打印新余额。由于该练习是与动态内存相关的章节的一部分,因此我使用了“new”和“delete”运算符。但出现了以下两个错误: LNK2019:在函数___tmainCRTStartup中引用未解析的外部符号WinMain@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;
}

我花了一个小时尝试理解这个问题,但没有成功。

3个回答

9

转到“链接器设置 -> 系统”页面。将“子系统”字段从“Windows”更改为“Console”。


3

看起来你正在编写一个标准控制台应用程序(有 int main()),但链接器却期望找到一个Windows入口点 WinMain

在项目的属性页中,链接器部分的 System/SubSystem 选项中,你是否选择了 "Windows (/SUBSYSTEM:WINDOWS)"?如果是,请尝试将其更改为 "Console (/SUBSYSTEM:CONSOLE)"。


2
创建新项目时,请选择“Win32控制台应用程序”而不是“Win32项目”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接