首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的代码什么也不返回,停止返回任何东西?

为什么我的代码什么也不返回,停止返回任何东西?
EN

Stack Overflow用户
提问于 2022-12-04 07:50:44
回答 1查看 35关注 0票数 -1

这是我家庭作业的一部分,我很难理解为什么输出是空的,然后代码就结束了。我并不真正理解类指针,所以如果对类指针如何影响代码有解释的话,它会有很大帮助。为什么我可以用秘书和接待员给employee_name打电话,但我不能用emp1或emp2来打电话。如果有人能把我和视频联系起来,我就能学到更多类似的话题,也能更好地理解课程,那就太好了。

头文件

代码语言:javascript
复制
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <string>

class Employee{
public:
  Employee(std::string n, double s);
  std::string employee_name;
  double salary;
};


class Department{
public:
  Department(std::string n, Employee* s, Employee* r);

  /*
  set the receptionist to r; note that both receptionist and r are pointers
  */
  void set_receptionist(Employee* r);

    /*
  set the secretary to s; note that both secretary and s are pointers
  */
  void set_secretary(Employee* s);

  /*
  calculate the total salary and return it. 
  neglect the receptionist or secretary if it is nullptr.
  count only once if receptionist and secretary point to the same employee (check their address instead of name!)
  */
  double get_total_salary() const;

  /*
  display the department information, including department name, employees, and total salaries.
  see details in main function.
  neglect the receptionist or secretary if it is nullptr.
  */
  void display_department() const;


private:
  std::string department_name;
  Employee* receptionist; 
  Employee* secretary;
};

#endif

实施文件

代码语言:javascript
复制
    Employee::Employee(std::string n, double s)
{
    employee_name = n;
    salary = s;
}

Department::Department(std::string n, Employee* s, Employee* r)
{
    department_name = n;
}

void Department::set_receptionist(Employee* r)
{
    receptionist = r;
}

void Department::set_secretary(Employee* s)
{
    secretary = s;
}

double Department::get_total_salary() const
{
    return 0.0;
}

void Department::display_department() const
{
    cout << "department name: " << department_name << endl;
    cout << "secretary name: " << secretary->employee_name << ", " << "salary: " << secretary->salary << endl;
    cout << "receptionist name: " << receptionist->employee_name << ", " << "salary: " << receptionist->salary << endl;
    //cout << "total salary: " <<  << endl;

}

主文件

代码语言:javascript
复制
    int main(){
  Employee emp1("Alice", 6000);
  Employee emp2("Bob", 5500);

  Department dep1("IT", &emp1, &emp2);
  dep1.display_department();
  /*
  department name: IT
  secretary name: Alice, salary: 6000
  receptionist name: Bob, salary: 5500
  total salary: 11500
  */

  dep1.set_receptionist(&emp1);
  dep1.display_department();
  /*
  department name: IT
  secretary name: Alice, salary: 6000
  receptionist name: Alice, salary: 6000
  total salary: 6000
  */

  dep1.set_secretary(nullptr);
  dep1.display_department();
  /*
  department name: IT
  receptionist name: Alice, salary: 6000
  total salary: 6000
  */

}

预期的输出在主文件的注释中。我试图找出Employee类的display_department,并且我知道get_total_salary是不正确的。

输出:

代码语言:javascript
复制
department name: IT
secretary name:

它输出这个,然后程序结束。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-04 08:11:07

您的问题是,在使用指针之前,您没有初始化或测试它们。

首先,在构造部门对象时,应该设置两个指针(这不是自动发生的)。

代码语言:javascript
复制
Department::Department(std::string n, Employee* s, Employee* r)
{
    department_name = n;
    secretary = s;
    receptionist = r;
}

然后,在尝试使用指针打印秘书或接待员名称之前,应该测试指针是否等于nullptr

代码语言:javascript
复制
void Department::display_department() const
{
    cout << "department name: " << department_name << endl;
    if (secretary == nullptr)
        cout << "no secretary:" << endl;
    else
        cout << "secretary name: " << secretary->employee_name << ", " << "salary: " << secretary->salary << endl;
    if (receptionist == nullptr)
        cout << "no receptionist:" << endl;
    else
        cout << "receptionist name: " << receptionist->employee_name << ", " << "salary: " << receptionist->salary << endl;
    //cout << "total salary: " <<  << endl;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74673694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档