【问题标题】:Passing class objects into function parameters c++将类对象传递给函数参数c ++
【发布时间】:2013-09-21 03:54:43
【问题描述】:

我已阅读有关此主题的几个先前提出的问题,但似乎无法找到我正在寻找的答案。当我运行程序时,我没有收到任何错误,但我得到了很多垃圾数据。我知道这是因为我没有正确传递参数,但我是 C++ 新手,特别是如何正确使用指针。为了简单起见:我通过PrintCheck()传递一个员工对象,它调用CalcSalary()函数,该函数必须使用GetHours()GetWage()来访问成员数据进行计算并返回正确的salary。希望能解释一下我为什么要生成垃圾数据!

我有一堂课:

class Employee
{
private:
    int employeeNumber;
    string name;
    string streetAddress;
    string phoneNumber;
    double hourlyWage;
    double hoursWorkedperWeek;
public:
    Employee(void);
    Employee(int, string, string, string, double, double);
    int GetEmployeeNum() const;
    void SetEmployeeNum(int);
    string GetName() const;
    void SetName(string);
    string GetAddress() const;
    void SetAddress(string);
    string GetPhone() const;
    void SetPhone(string);
    double GetWage() const;
    void SetWage(double);
    double GetHours() const;
    void SetHours(double);
    double CalcPay(double, double);
};

我还有一个函数需要和类交互:

void PrintCheck(Employee&);

我的主要功能如下:

void main()
{
    Employee joe(1, "Joe Blo", "125 Cool St.", "555 555 5555", 10.00, 45); //create employee 1
    Employee jane(2, "Jane Doe", "521 Dumb St.", "1 800 555 5555", 12.50, 30); //create employee 2
    PrintCheck(joe); //print check
}

printcheck 函数如下所示:

void PrintCheck(Employee& a)
{

    cout << "Pay to the order of " << a.GetName() << "...................................";
    cout << a.CalcPay(a.GetWage(), a.GetHours()) << endl;
    cout << "Hours worked: " << a.GetHours() << endl;
    cout << "Hourly wage: " << a.GetWage() << endl;
}

Calcpay功能是:

double Employee::CalcPay(double h, double w)
{
    double salary = 0;
    int OT = 40;
    double timeandahalf = 1.5;
    double STATE = .075;
    double FED = .20;
    if (h > OT) // overtime
    {
        salary = h * (w * timeandahalf); // calc time and a half
        salary = salary * STATE; // calc state deductions
        salary = salary * FED; // calc federal deductions
    }
    else 
    {
        salary = h * w; // calc salary
        salary = salary * STATE; // calc state deductions
        salary = salary * FED; // calc federal deductions
    }
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(PRECISION);
    return salary;
}

我的“get”函数都遵循这个模式:

int Employee::GetEmployeeNum() const
{
    return employeeNumber;
}

我的预期输出是:

Pay to the order of:  Joe Blo............ $salary.
Hours worked: $hours.
Hourly wage: $wage.

我得到了什么:

Pay to the order of: ........................ 128509280503000000000000000000000000000.00 (this number is long, but I didn't feel like typing them all)
Hours worked: -9723636237 (same, just tons of bs numbers)
Hourly wage: (the exact same number as above)

我的类构造函数:

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    int employeeNumber = en;
    string name = n;
    string streetAddress = a;
    string phoneNumber = p;
    double hourlyWage = w;
    double hoursWorkedperWeek = h;
}
Employee::Employee()
{
}

【问题讨论】:

  • 为什么不在您的问题中包含您期望的输出和实际得到的输出?
  • 构造函数Employee(int, string, string, string, double, double) 的实现是什么样的?
  • 注意你的语言。这次我会为你编辑它,但将来有一个模组可以删除你的帖子,甚至不眨眼。
  • 您的构造函数是否正确地初始化了实例?
  • 对不起语言,你知道错误是多么令人沮丧!我在帖子底部的构造函数中进行了编辑。

标签: c++ class parameter-passing


【解决方案1】:

这是错误

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    int employeeNumber = en;
    string name = n;
    string streetAddress = a;
    string phoneNumber = p;
    double hourlyWage = w;
    double hoursWorkedperWeek = h;
}

应该是

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    employeeNumber = en;
    name = n;
    streetAddress = a;
    phoneNumber = p;
    hourlyWage = w;
    hoursWorkedperWeek = h;
}

或者更好的是,它应该像greatwolf的回答那样。

您的版本中的错误是您在构造函数中声明的变量与您的类成员的名称完全相同。这些变量隐藏你的类成员,所以你的构造函数不会初始化你的类。所以你得到了垃圾。

【讨论】:

  • 感谢您的帮助。我不敢相信这个问题是如此愚蠢且容易被忽视。总的来说,我的理解是更大的。再次感谢。
  • @xavi C++ 充满了陷阱。
【解决方案2】:

您的Employee::Employee(int en, string n, string a, string p, double w, double h) 在其中声明了新的局部变量,因此隐藏了类中的成员变量。所以在现实中,你从来没有在构造过程中正确初始化它的任何成员。

以下内容应该可以解决这个问题:

Employee::Employee(int en, string n, string a, string p, double w, double h)
  : employeeNumber ( en ),
    name ( n ),
    streetAddress ( a ),
    phoneNumber ( p ),
    hourlyWage ( w ),
    hoursWorkedperWeek ( h )
{
}

【讨论】:

  • 这不起作用。当我尝试这个时,编译器无法识别任何数据成员。
  • 构建错误说“在应该是逗号分隔的列表中找到”。在每一行。
  • @xavi 哎呀,在复制/粘贴过程中忘记修复逗号,立即尝试
  • 你会看那个!完美运行。你能解释一下为什么这个语法允许我的数据工作吗?
  • @xavi 这是使用initializer list
猜你喜欢
  • 2015-02-01
  • 2015-11-22
  • 1970-01-01
  • 2021-05-08
  • 2015-08-30
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2016-12-31
相关资源
最近更新 更多