You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2022编译C++贷款计算器遇LNK2019/LNK1120错误求助

贷款计算器编译错误(LNK2019/LNK1120)解决

问题描述

我正在为项目开发一款早期版本的贷款计算器,使用Visual Studio 2022社区版编译时,遇到了无法理解的LNK2019和LNK1120错误。我是C++及编程领域的新手,此前仅学习过C语言,恳请各位提供技术帮助。

原代码

#include <fstream>
#include <string>
#include <iomanip>

using std::cout;
using std::endl;
using std::cin;

void welcome();
double getPrincipal();
double getTerm();
double getAnnualRate();
double convertRate();
double CalcPayment();
void DisplayResults(double, double, double, double);

int main()
{
    double loanPrincipal(0);
    double termLength(0);
    double annualIntRate(0);
    double monthlyIntRate(0);
    double MonPayment(0);

    welcome();
    loanPrincipal = getPrincipal();
    termLength = getTerm();
    annualIntRate = getAnnualRate();
    monthlyIntRate = convertRate();
    MonPayment = CalcPayment();
    DisplayResults(loanPrincipal, termLength, annualIntRate, MonPayment);

    return 0;
}

#include <iostream>
#include <string>
#include <iomanip>

//welcomes the user
void welcome()
{
    cout << "Welcome to the ammortization loan calculator" << endl;
    cout << "" << endl;
}

//asks the user for amount of loan
double getPrincipal()
{
    double userPrinicpal = 0;

    cout << "Please enter the loan ammount that you wish to borrow: " << endl;
    double userPrincipal;
    cin >> userPrincipal;

    return userPrincipal;
}

//asks the user for the annual interest rate
double getAnnualRate(double annualIntRate)
{
    cout << "Please enter the annual interest rate: " << endl;
    cin >> annualIntRate;

    return annualIntRate;
}

double convertRate(const double annualIntRate)
{
    double MonIntRate;
    MonIntRate = (annualIntRate / 100) / 12;

    return MonIntRate;
}

//asks the user for the length of the loan
void getTerm(int& termLength)
{
    cout << "Please enter the length of the loan (10, 15, or 30 years)" << endl;
    cin >> termLength;
}

//calculate monthly payment
double CalcPayment(double loanPrincipal, double monthlyIntRate, int termLength)
{
    double MonPayment = loanPrincipal * (monthlyIntRate * (1 + monthlyIntRate) * 180) / ((1 + 0.00375) * 180 - 1);

    return MonPayment;
}

//display results from previous calculations to the user
void DisplayResults(double loanPrincipal, double termLength, double annualIntRate, double MonPayment)
{
    cout << "Loan Application - Amortization Schedule" << endl;
    cout << "" << endl;
    cout << "Principal:                      " << loanPrincipal << endl;
    cout << "Life of Loan:                   " << termLength << endl;
    cout << "Annual interest rate:           " << annualIntRate << endl;
    cout << "Monthly Payment:                " << MonPayment << endl;
}

错误信息

Error   LNK2019 unresolved external symbol "double __cdecl getTerm(void)" (?getTerm@@YANXZ) referenced in function main

Error   LNK2019 unresolved external symbol "double __cdecl getAnnualRate(void)" (?getAnnualRate@@YANXZ) referenced in function main

Error   LNK2019 unresolved external symbol "double __cdecl convertRate(void)" (?convertRate@@YANXZ) referenced in function

Error   LNK2019 unresolved external symbol "double __cdecl CalcPayment(void)" (?CalcPayment@@YANXZ) referenced in function

Error   LNK1120 4 unresolved externals

错误原因

所有LNK2019错误都源于函数声明和定义不匹配:编译器根据函数声明查找对应实现,但声明的参数、返回值和实际定义完全不一致,导致无法找到匹配的函数体。

逐个问题拆解:

  1. getTerm:声明是double getTerm()(无参数、返回double),但定义是void getTerm(int& termLength)(void返回、传int引用),main调用逻辑和定义完全冲突。
  2. getAnnualRate:声明是double getAnnualRate()(无参数),但定义是double getAnnualRate(double annualIntRate)(需要一个double参数),调用时未传参,找不到对应实现。
  3. convertRate:声明是double convertRate()(无参数),定义是double convertRate(const double annualIntRate)(需要一个double参数),调用时未传参,不匹配。
  4. CalcPayment:声明是double CalcPayment()(无参数),定义是double CalcPayment(double loanPrincipal, double monthlyIntRate, int termLength)(需要三个参数),调用时未传参,找不到实现。

额外问题:main中termLength是double类型,但getTerm定义里是int引用,类型不统一。

修正后的代码

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>  // 用于pow函数

using std::cout;
using std::endl;
using std::cin;

// 修正函数声明,与定义严格匹配
void welcome();
double getPrincipal();
int getTerm();  // 贷款年限为整数,返回int类型
double getAnnualRate();
double convertRate(double annualIntRate);  // 添加必要参数
double CalcPayment(double loanPrincipal, double monthlyIntRate, int termLength);
void DisplayResults(double, int, double, double);  // 调整termLength类型

int main()
{
    double loanPrincipal(0);
    int termLength(0);  // 改为int类型,匹配getTerm返回值
    double annualIntRate(0);
    double monthlyIntRate(0);
    double MonPayment(0);

    welcome();
    loanPrincipal = getPrincipal();
    termLength = getTerm();
    annualIntRate = getAnnualRate();
    monthlyIntRate = convertRate(annualIntRate);  // 传入参数
    MonPayment = CalcPayment(loanPrincipal, monthlyIntRate, termLength);  // 传入参数
    DisplayResults(loanPrincipal, termLength, annualIntRate, MonPayment);

    return 0;
}

//welcomes the user
void welcome()
{
    cout << "Welcome to the ammortization loan calculator" << endl;
    cout << endl;
}

//asks the user for amount of loan
double getPrincipal()
{
    cout << "Please enter the loan amount that you wish to borrow: " << endl;
    double userPrincipal;
    cin >> userPrincipal;

    return userPrincipal;
}

//asks the user for the annual interest rate
double getAnnualRate()  // 去掉多余参数,与声明匹配
{
    double annualIntRate;
    cout << "Please enter the annual interest rate: " << endl;
    cin >> annualIntRate;

    return annualIntRate;
}

double convertRate(const double annualIntRate)
{
    return (annualIntRate / 100.0) / 12.0;  // 简化写法
}

//asks the user for the length of the loan
int getTerm()  // 修改返回值为int,去掉引用参数,与声明匹配
{
    int termLength;
    cout << "Please enter the length of the loan (10, 15, or 30 years)" << endl;
    cin >> termLength;

    return termLength;
}

//calculate monthly payment
double CalcPayment(double loanPrincipal, double monthlyIntRate, int termLength)
{
    // 修正公式,使用动态计算的参数,替换写死的数值
    int totalMonths = termLength * 12;
    double compoundFactor = pow(1 + monthlyIntRate, totalMonths);
    return loanPrincipal * (monthlyIntRate * compoundFactor) / (compoundFactor - 1);
}

//display results from previous calculations to the user
void DisplayResults(double loanPrincipal, int termLength, double annualIntRate, double MonPayment)
{
    cout << "Loan Application - Amortization Schedule" << endl;
    cout << endl;
    cout << std::fixed << std::setprecision(2);  // 格式化金额输出
    cout << "Principal:                      $" << loanPrincipal << endl;
    cout << "Life of Loan:                   " << termLength << " years" << endl;
    cout << "Annual interest rate:           " << annualIntRate << "%" << endl;
    cout << "Monthly Payment:                $" << MonPayment << endl;
}

关键修正点

  • 强制统一所有函数的声明与定义:参数个数、类型、返回值完全一致。
  • 修正CalcPayment的计算公式:替换写死的数值为动态参数,使用pow函数计算复利因子。
  • 统一变量类型:贷款年限改为int,符合实际业务逻辑。
  • 优化输出格式:添加fixed和setprecision让金额显示更规范。

内容的提问来源于stack exchange,提问作者Kaleb Prichard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:54:26