C ++ LNK2019和LNK1120错误 [英] C++ LNK2019 and LNK1120 errors

查看:193
本文介绍了C ++ LNK2019和LNK1120错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Deitel的书里做另一个练习。该程序计算每月利息,并为每个储户打印新的余额。由于练习是与动态内存相关的章节的一部分,我使用新和删除运算符。由于某些原因,我得到这两个错误:

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:未解析的外部符号WinMain @ 16在函数___ tmainCRTStartup

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

致命错误LNK1120:1未解析的外部

fatal error LNK1120: 1 unresolved externals

这是类头文件。

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

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

我花了一个小时试图找出这一点, p>

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

推荐答案

转到链接器设置 - >系统。将Subsystem字段从Windows更改为控制台。

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

这篇关于C ++ LNK2019和LNK1120错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆