【问题标题】:Not understanding C++ argument passing via functions不理解通过函数传递的 C++ 参数
【发布时间】:2016-08-18 12:30:54
【问题描述】:

我是 C++ 新手,我正在尝试了解如何通过多个函数传递变量。我知道您可以使用全局变量或参数传递。

在我的代码中,我在主函数之后定义了两个函数。我已经用原型设置了它。我在 CodeBlocks 中收到的错误是 .error: 'studentFees' was not declared in this scope。这是有道理的,因为我没有在123124 行中实现任何东西。但我不太确定如何。我对正确实现原型也感觉不太好。

有人可以帮助我以正确的方式执行此操作吗?

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

void undergradBill(
               double finalBill = 0;
               double studentTuition,
               double studentFees,
               double studentID,
               string studentLevel,
               string studentBio,
               string studentRes,
               string studentLife,
               string studentCredit
                );
void gradBill(
               double finalBill = 0;
               double studentTuition,
               double studentFees,
               double studentID,
               string studentLevel,
               string studentBio,
               string studentRes,
               string studentLife,
               string studentCredit
              );

int main()
{
    double finalBill = 0;
    double studentTuition = 0;
    double studentFees = 0;
    double studentID = 0;
    string studentLevel = "";
    string studentBio = "";
    string studentRes = "";
    string studentLife = "";
    string studentCredit = "";

    cout << "Welcome College Student!" << endl;
    cout << "This program is designed to assist you in calculating your college tuition per semester." << endl;
    cout << endl;
    cout << "Please provide the following information:" << endl;
    cout << "   -Student ID" << endl;
    cout << "   -Graduate/Undergraduate" << endl;
    cout << "   -Residency" << endl;
    cout << "   -Major" << endl;
    cout << "   -Full Time/Part Time" << endl;
    cout << "   -Credits taken this semester" << endl;
    cout << endl;
    system("PAUSE");

    system("CLS");
    cout << "Please enter your student ID." << endl;
    cout << "Student ID: ";
    cin >> studentID;
    while(cin.fail()) {
        cout << "Error: please enter a valid entry." << endl;
        cin.clear();
        cin.ignore(256,'\n');
        cout << "Student ID :";
        cin >> studentID;
    }

    cout << endl;
    cout << "Are you a graduate or undergraduate student?" << endl;
    cout << "(G/U) :";
    cin.get();
    getline(cin, studentLevel);
    while(studentLevel != "g" && studentLevel != "G" && studentLevel != "u" && studentLevel != "U") {
        cout << "Error: please enter a valid entry." << endl;
        cout << "(G/U) :";
        getline(cin, studentLevel);
    }
    if(studentLevel == "g" || studentLevel == "G") {
        cout << endl;
        cout << "Are you apart of the biology program?" << endl;
        cout << "(Y/N) :";
        getline(cin, studentBio);
        while(studentBio != "y" && studentBio != "Y" && studentBio != "n" && studentBio != "N") {
        cout << "Error: please enter a valid entry." << endl;
        cout << "(Y/N) :";
        getline(cin, studentBio);
        }
    }

    cout << endl;
    cout << "Are you a resident or New York State?" << endl;
    cout << "(Y/N) :";
    getline(cin, studentRes);
    while(studentRes != "y" && studentRes != "Y" && studentRes != "n" && studentRes != "N") {
        cout << "Error: please enter a valid entry" << endl;
        cout << "(Y/N) :";
        getline(cin, studentRes);
    }

    cout << endl;
    cout << "Are you a full time student or a part time student?" << endl;
    cout << "(F/P) :";
    getline(cin, studentLife);
    while(studentLife != "f" && studentLife != "F" && studentLife != "p" && studentLife != "P") {
        cout << "Error: please enter a valid entry." << endl;
        cout << "(F/P) :";
        getline(cin, studentLife);
    }
    if (studentLife == "p" || studentLife == "P") {
        cout << endl;
        cout << "How many credit hours are you taking this semester?" << endl;
        cout << "Credit Hours :";
        cin >> studentCredit;
        while(cin.fail()) {
        cout << "Error: please enter a valid entry." << endl;
        cin.clear();
        cin.ignore(256,'\n');
        cout << "Credit Hours :";
        cin >> studentCredit;
        }
    }

    if(studentLevel == "u" || studentLevel == "U" || studentLife == "p" || studentLife == "P") {undergradBill();}
    else {gradBill();}

    system("CLS");
    finalBill = studentTuition + studentFees;
    cout << "Student Account: " << studentID << endl;
    cout << "Billing Total: " << finalBill << endl;
}

void undergradBill() {
    if(studentLife == "f" || studentLife == "F") {
        if(studentRes == "y" && studentRes == "Y") {
            studentTuition = 3085.00;
            studentFees = 588.50;
        }
        else {
            studentTuition = 7910.00;
            studentFees = 588.50;
        }
    }
    else {
        if(studentRes == "y" && studentRes == "Y") {
            studentTuition = 257.00;
            studentFees = studentCredit * 48.95;
        }
        else {
            studentTuition = 659.00;
            studentFees = studentCredit * 48.95;
        }
    }
}

void gradBill() {
    if(studentBio == "y" || studentBio == "Y") {
        if(studentLife == "f" || studentLife == "F") {
            if(studentRes == "y" && studentRes == "Y") {
                studentTuition = 5185.00;
                studentFees =  342.14 + 900.00;
            }
            else {
                studentTuition = 10095.00;
                studentFees = 342.14 + 900.00;
            }
        }
        else {
            if(studentRes == "y" && studentRes == "Y") {
                studentTuition = studentCredit  * 432.00;
                studentFees = (studentCredit * 28.37) + 900.00;
            }
            else {
                studentTuition = studentCredit * 841.00;
                studentFees = (studentCredit * 28.37) + 900.00;
            }
        }
    }
    else {
        if(studentLife == "f" || studentLife == "F") {
            if(studentRes == "y" && studentRes == "Y") {
                studentTuition = 5185.00;
                studentFees =  342.14;
            }
            else {
                studentTuition = 10095.00;
                studentFees = 342.14;
            }
        }
        else {
            if(studentRes == "y" && studentRes == "Y") {
                studentTuition = studentCredit  * 432.00;
                studentFees = studentCredit * 28.37;
            }
            else {
                studentTuition = studentCredit * 841.00;
                studentFees = studentCredit * 28.37;
            }
        }
    }
}

我知道有很多问题要问... 感谢任何可以帮助我了解如何更好地做到这一点的人!

【问题讨论】:

  • 将原型中的参数添加到 gradBill() 和 undergradBill() 的实现中。
  • 您需要start with a good beginners book,因为这些函数声明在 C++ 中无效,应该会给您错误。
  • 具有默认参数的参数必须在参数列表的末尾形成一个连续的组。因此,如果您想为 finalBill 提供默认参数,则应将其作为最后一个参数传递,或者您也应为以下参数提供默认值。

标签: c++ function arguments global-variables parameter-passing


【解决方案1】:

问题在于,在 C++ 中,您有 函数重载,这意味着您可以拥有两个具有相同名称但不同 签名(基本上是参数)的函数。

例如,如果你有一个函数

void undergradBill(double finalBill);

不同于函数

void undergradBill();

您已经声明(我可能会非法添加)带参数的函数声明。然后你定义了带有 no 参数的函数。由于重载,这在 C++ 中有效。

所以你的问题有很多原因:首先你有非法的函数声明,这会给你带来错误。因此,编译器没有您要调用的函数的声明。由于您的声明和定义不匹配的问题,您将无法调用这些函数,因为您调用的函数实际上并不存在。

当然,由于您定义的函数没有参数,使用“参数”会产生错误,因为函数定义中不存在这些变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多