我试图做从Deitel公司的书另一个练习。程序计算,每月利息并打印新的余额为每个储户的。由于演习是涉及到动态内存的一章的一部分,我使用的“新”和“删除”操作符。出于某种原因,我得到这两个错误:

  

LNK2019:解析的外部符号的WinMain @ 16在函数引用___ tmainCRTStartup

     

致命错误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;
}

我花了一个小时的努力没有成功摸不着头脑。

有帮助吗?

解决方案

转到 “接头设置 - >系统”。从“视窗”到“控制台”更改的字段“子系统”。

其他提示

它看起来像你正在编写一个标准的控制台应用程序(你有int main()),但该接头希望找到一个窗口切入点WinMain

在YOUT项目的属性页中,接头部分,系统/子系统选项,你有没有“的Windows(/子系统:WINDOWS)”选择?如果是这样,尝试改变为 “控制台(/ SUBSYSTEM:CONSOLE)”

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top