C++ LNK1120 and LNK2019 errors: “unresolved external symbol WinMain@16”

蹲街弑〆低调 提交于 2019-12-23 09:27:50

问题


I'm trying to do another exercise from Deitel's book. The program calculates the monthly interest and prints the new balances for each of the savers. As the exercise is part of the chapter related to dynamic memory, I'm using "new" and "delete" operators. For some reason, I get these two errors:

LNK2019: unresolved external symbol WinMain@16 referenced in function ___tmainCRTStartup

fatal error LNK1120: 1 unresolved externals

Here is class header file.

//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 file with member function definitions

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

End finally driver program :

#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;
}

I have spent an hour trying to figure this out with no success.


回答1:


Go to "Linker settings -> System". Change the field "Subsystem" from "Windows" to "Console".




回答2:


It looks like you are writing a standard console application (you have int main()), but that the linker is expecting to find a windows entry point WinMain.

In yout project's property pages, in the Linker section, System/SubSystem option, do you have "Windows (/SUBSYSTEM:WINDOWS)" selected? If so, try changing it to "Console (/SUBSYSTEM:CONSOLE)"




回答3:


When creating a new project, select "Win32 Console Application" instead of "Win32 Project".



来源:https://stackoverflow.com/questions/1252049/c-lnk1120-and-lnk2019-errors-unresolved-external-symbol-winmain16

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!