我一直在为我的C ++函数调试错误。我不确定我做错了什么

时间:2012-10-14 20:41:35

标签: c++ function

嗨,我对编程非常陌生,对于这个程序,我应该能够通过使用函数来计算某人的月费。当我编译它很好,但是当我尝试调试它时说我失败了所以我不知道我做错了什么。这是我的代码:

//此程序计算用户的每月费用

#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void showpackage();
void calculatePkg_A (int,int,int);
void calculatePkg_B (int,int,int);
void calculatePkg_C (int,int,int);


int main ()
{
    int inputPackage; //package choice
    double inputHours; //number of hours


    //constants for package choice
    const int pkg_A = '1' ,
               pkg_B = '2',
               pkg_C = '3';
     const char quit_choice = 'Q' || 'q';
    //constants for cost of package
    const int cost_A = 15,
              cost_B = 20,
              cost_C = 25;
    //constants for package access time
    const int hours_A = 50,
              hours_B = 100,
              hours_C = 150;
    cout << fixed << showpoint << setprecision (2);
    do
    { //display package options. 
        showpackage();
        cin >> inputPackage;

        //validate package selection
        while (inputPackage < pkg_A || inputPackage > pkg_C || inputPackage != quit_choice )
        {
            cout << "Please enter a valid package choice:";
            cin >> inputPackage;
        }
        //If user does not want to quit, proceed.
if (inputHours < 0 || inputHours > 720 || inputPackage != quit_choice)
        {
            //get the number of hours used
            cout << "How many hours were used?";
            cin >> inputHours;

                //display the total charges
                switch (inputPackage)
                {
                case pkg_A:
                    calculatePkg_A (cost_A, hours_A, inputHours);
                    break;
                case pkg_B:
                    calculatePkg_B (cost_B, hours_B, inputHours);
                    break;
                case pkg_C:
                    calculatePkg_C (cost_C, hours_C, inputHours);
                    break;
                }
            }
    } while (inputPackage != quit_choice);


            return 0;
}


//definition of function showPackage which displays package options

void showPackage()
{cout <<"An Internet service provider has three different packages for its customers: \n"
<<"1.Package A: For $15 per month with 50 Hours of access provided, additional hours \n"
<<"are $2.00 per hour over 50 hours.\n"
<<"2.Package B: For $20 per month with 100 Hours of access provided, additional hours \n"
<<"are $1.50 per hour over 100 hours.\n"
<<"3.Package C: For $25 per month with 150 Hours of access provided, additional hours \n"
<<"are $1.00 per hour over 150 hours.\n"
<<"Enter your choice either 1,2, or 3:";
}

//defintion of function calculatePkg_A. Displays total charges.


void calculatePkg_A (int cost_A, int hours_A, int inputHours)
    {
        cout << "The total charges are $"
             << (cost_A += (inputHours - hours_A) * 2) << endl;
}

//
//defintion of function calculatePkg_B. Displays total charges.
//

void calculatePkg_B (int cost_B, int hours_B, int inputHours)
    {
        cout << "The total charges are $"
             << (cost_B += (inputHours - hours_B) * 1.5) << endl;
}

//defintion of function calculatePkg_C. Displays total charges.


void calculatePkg_C (int cost_C, int hours_C, int inputHours)
    {
        cout << "The total charges are $"
             << (cost_C += (inputHours - hours_C)) << endl;
}

但每当我尝试调试它时,我都会收到错误消息:

1>Program5.obj : error LNK2019: unresolved external symbol "void __cdecl calculatePkg_C(double,double,double)" (?calculatePkg_C@@YAXNNN@Z) referenced in function _main
1>Program5.obj : error LNK2019: unresolved external symbol "void __cdecl showpackage(void)" (?showpackage@@YAXXZ) referenced in function _main
    1>C:\Users\charlotte\Desktop\program5\Debug\program5.exe : fatal error LNK1120: 2 unresolved externals 

4 个答案:

答案 0 :(得分:1)

错误消息告诉我们您已声明calculatePkg_C(double,double,double),但您已实施calculatePkg_C(int,int,int)。在定义中更改原型,它应该工作。另外,查看声明此原型的函数的位置。

答案 1 :(得分:1)

第一个错误是编译器决定您需要将double类型传递给calculatePkg_C。它期望:

calculatePkg_C(double,double,double)

但你的是:

calculatePkg_C(int,int,int)

你需要问问自己为什么会这样。您已将其声明为顶部,并在下方正确定义。有没有你没有展示的东西?我注意到你正在通过inputHours double。尝试将其转换为int

另一个错误是showPackage。您的声明(和用法)的大小写错误。

答案 2 :(得分:0)

您需要检查每种构建类型的构建属性。包含caclulatePkg_C()showpackage()方法的源文件位于Release配置中,但不在您的Debug配置中。

答案 3 :(得分:0)

我很遗憾你宣称你的三个函数在他们的原型中采用了三个int参数:

void calculatePkg_A (int,int,int);
void calculatePkg_B (int,int,int);
void calculatePkg_C (int,int,int);

但你确实将它作为第三个参数传递给它,因为你声明:

double inputHours; //number of hours

我不推荐任何演员,如果你必须保留函数原型,最好将它声明为整数。

在showpackage实现中,记住C / C ++有一个区分大小写的sintax,因此你应该写:

//definition of function showPackage which displays package options

void showPackage()
{
...
}