找不到未解析的外部符号(c ++)

时间:2013-12-10 04:07:45

标签: c++ visual-c++ unresolved-external

为课程做作业。收到此错误消息:

    1>------ Build started: Project: Mulvihill_Program_7, Configuration: Debug Win32 ------
    1>Mulvihill_Program_7.obj : error LNK2019: unresolved external symbol "double __cdecl calcGross(void)" (?calcGross@@YANXZ) referenced in function _main
    1>c:\users\pat\documents\visual studio 2012\Projects\Mulvihill_Program_7\Debug\Mulvihill_Program_7.exe : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

以下是代码:

#include<iostream>
using namespace std;

int getHoursWorked();
double getPayRate();
double calcGross();

int hoursWorked = 0;
double payRate = 0.0;
double grossPay = 0.0;
double OVERTIME = 1.5;
double overTimePayRate = 0.0;
double pay = 0.0;



int main()
{

    getHoursWorked();
    getPayRate();
    pay = calcGross();

    cout<<pay;
}

int getHoursWorked()
{
    cout<<"Enter the amount of hours worked ";
    cin>>hoursWorked;

    return(hoursWorked);
}

double getPayRate()
{
    cout<<"How much do you make an hour? ";
    cin>>payRate;

    return(payRate);

}

double calcGross(int hoursWorked, double payRate)
{
    if (payRate < 40)
    {
        grossPay = hoursWorked * payRate;
    }
    else
    {
        overTimePayRate = OVERTIME * payRate;
        grossPay = hoursWorked * overTimePayRate;
    }
    return(grossPay);
}

我知道这个代码可能有很多错误,而不仅仅是常规和错误方面的错误,但我是新手,我只是想了解错误信息。

2 个答案:

答案 0 :(得分:1)

链接器无法找到此函数定义

double calcGross();

因为您定义了这样的函数,

double calcGross(int hoursWorked, double payRate)

你应该删除参数,以便函数声明&amp;定义是匹配的。 像这样更改你的函数声明,

int getHoursWorked();
double getPayRate();
double calcGross(int hoursWorked, double payRate);

然后传递hoursWorked&amp;的价值在payRate

中调用时main()

答案 1 :(得分:0)

您的calcGross()定义与main()中调用的类型签名具有不同的类型签名。 main()调用一个calcGross(),不带任何参数,这个参数与calcGross()完全不同,有两个参数 - int和double。

添加

double calcGross()
{
    // replace with your logic
    return 0.0;
}

代码,你不会得到任何链接器错误。或者为calcGross(int,double)定义提供必需的参数。

有关其他详细信息,请参阅Wiki中的type signature