返回指向对象数组的指针并将其指定给指针

时间:2014-04-02 01:22:30

标签: c++ arrays pointers

我遇到了返回指向对象数组的指针的问题。在输入所有信息并顺利进行之后,当我尝试仅使用指针打印出对象数组时,第一条记录正确打印出来,下一条记录只是混乱的数字。我认为问题在于获得公司员工的回报和分配。现在,我认为只有第一条记录从get_employees()函数返回。我在这方面错了,或者它也可能将新指针分配给创建的数据库。我无法弄清楚问题是什么。我知道数组的名称是指向第一条记录的指针,我只是不知道怎么不输?其他记录。

//#include "stdafx.h" // Used only on Windows machines
#include <iostream>
#include <cstdlib>
#include <new>
#include <cstring>
using namespace std;

//**********************************************************************
//*                         Symbolic Constants                         *
//**********************************************************************

#define MAX_NAME_LENGTH    80      // Maximum company name length
#define COMPANY_ALLOC_ERR  1       // Company name memory allocation err
#define NAME_ALLOC_ERR     2       // Name allocation error
#define EMPLOYEE_ALLOC_ERR 3       // Employee allocation error

//**********************************************************************
//*                          Program Structure                         *
//**********************************************************************
//
struct company_information
{
   char  *p_company_name;
   int   bonus_year,
         number_of_employees;
   float bonus_amount;
};

//**********************************************************************
//*                            Program Class                           *
//**********************************************************************
//
class company_employee_bonus_record
{
   int   employee_id,
         service_years,
         year_hired;
   float bonus;
public:

   // Set the member variables
   void  set_id           (int i)   {employee_id   = i;}
   void  set_service_years(int s)   {service_years = s;}
   void  set_year_hired   (int y)   {year_hired    = y;}
   void  set_bonus        (float b) {bonus         = b;}

   // Get the member variables
   int   get_id           ()        {return employee_id;}
   int   get_service_years()        {return service_years;}
   int   get_year_hired   ()        {return year_hired;}
   float get_bonus        ()        {return bonus;}

   ~company_employee_bonus_record() {cout << "Destructor executing";}
};

//**********************************************************************
//*                        Function prototypes                         *
//**********************************************************************
company_information get_company_information();
company_employee_bonus_record get_employees(company_information user_company);
void print_employee_database(company_employee_bonus_record *p_employee_db, int number_emp);

//**********************************************************************
//*                           Main function                            *
//**********************************************************************
int main()
{
   company_employee_bonus_record *p_user_employee_db;
   company_information user_company;


   user_company = get_company_information();

   cout << "\nCompany name: "        << user_company.p_company_name;
   cout << "\nYear of te bonuses: "  << user_company.bonus_year;
   cout << "\nNumber of employees: " << user_company.number_of_employees;
   cout << "\nBonus per year: "      << user_company.bonus_amount;

   // This is the part that is confusing me up.

   *p_user_employee_db = get_employees(user_company);

   print_employee_database(p_user_employee_db, user_company.number_of_employees);

   return 0;
}


//**********************************************************************
//*                      Get company information                       *
//**********************************************************************
company_information get_company_information()
{
   company_information *p_company_info;
   char company_name[MAX_NAME_LENGTH + 1];

   try {
      p_company_info = new company_information;
   } catch (bad_alloc xa) {
      exit(COMPANY_ALLOC_ERR);
   }

   cout << "\nEnter the name of your company here (no spaces): ";
   cin  >> company_name;

   try {
      p_company_info->p_company_name = new char[strlen(company_name) + 1];
   } catch (bad_alloc xa) {
      exit(NAME_ALLOC_ERR);
   }

   strcpy(p_company_info->p_company_name, company_name);
   cout << "Enter your number of employees (1 or more): ";
   cin  >> p_company_info->number_of_employees;
   cout << "Enter the year in which the bonuses are given (YYYY): ";
   cin  >> p_company_info->bonus_year;
   cout << "Give the yearly bonus amount per employee (in dollars): ";
   cin  >> p_company_info->bonus_amount;

   return *p_company_info;
}

company_employee_bonus_record get_employees(company_information user_company)
{
   int service_years, employee_number;
   company_employee_bonus_record *p_employee_db,
                                 *p_next_employee;
   try {
      p_employee_db = new company_employee_bonus_record[user_company.number_of_employees];
   } catch (bad_alloc xa) {
      exit(EMPLOYEE_ALLOC_ERR);
   }

   employee_number = 1;
   p_next_employee = p_employee_db;

   do {
      cout << "\nPlease enter the number of years for employee" << employee_number
            << "(0 if emp doesnt exist): ";
      do {
         cin >> service_years;
         if (service_years < 0)
         {
            cout << "Invalid year";
         }
      } while (service_years < 0);
      if(service_years > 0)
      {
         p_next_employee->set_id(employee_number);
         p_next_employee->set_service_years(service_years);
         p_next_employee->set_year_hired(user_company.bonus_year - service_years);
         p_next_employee->set_bonus(user_company.bonus_amount * service_years);

         employee_number++;
         p_next_employee++;
      }
      else{
         employee_number++;
      }
   } while (p_next_employee - p_employee_db < user_company.number_of_employees);

   return *p_employee_db;
}

void print_employee_database(company_employee_bonus_record *p_employee_db, int number_emp)
{
   company_employee_bonus_record *p_next_employee;

   p_next_employee = p_employee_db;

   cout << "\nUNSORTED LIST";

   //In this while loop, the first record is sorted fine, but the next ones are all jumbled numbers.

   while(p_next_employee - p_employee_db < number_emp)
   {
      cout << "\n" << p_next_employee->get_id() << "     "
      << p_next_employee->get_service_years() << "   "
      << p_next_employee->get_year_hired() << "    "
      << p_next_employee->get_bonus();
      p_next_employee++;
   }
}

2 个答案:

答案 0 :(得分:1)

有两种方法可以解决您的问题。

  1. 返回指向存储在数据库中的对象的指针。
  2. 从数据库中存储的对象列表中返回一个对象。
  3. 要简化此答案,请“使用简单的struct

    struct A
    {
    };
    
    A database[DATABASE_SIZE]; // Let's assume DATABASE_SIZE is defined somewhere.
    
    A* getPointerFromDataBase()
    {
       // Return the 101-st object from the database
       return database+100;
    }
    
    A& getObjectFromDataBase()
    {
      // Return the 101-st object from the database
      return database[100];
    }
    
    int main()
    {
       // Get a pointer from the database.
       A* aPtr = getPointeFromDataBase();
    
       // Get an object from the database.
       A a = getObjectFromDataBase();
    
       // This is no valid. aPtr2 does not point to any valid address.
       // It cannot be dereferenced to hold an object.
       A* aPtr2;
       *aPtr2 = getObjectFromDataBase();
    }
    

答案 1 :(得分:0)

以下是上述发布版本与工作版本之间运行差异的输出:

 1c1
< //#include "stdafx.h" // Used only on Windows machines
---
> 
5c5
< #include <cstring>
---
> #include <string>
23c23
<    char  *p_company_name;
---
>    string company_name;
60c60,61
< company_employee_bonus_record get_employees(company_information user_company);
---
> company_employee_bonus_record *get_employees(company_information user_company);
> 
74c75
<    cout << "\nCompany name: "        << user_company.p_company_name;
---
>    cout << "\nCompany name: "        << user_company.company_name;
81c82
<    *p_user_employee_db = get_employees(user_company);
---
>    p_user_employee_db = get_employees(user_company);
94c95
<    company_information *p_company_info;
---
>    company_information company_info;
97,102d97
<    try {
<       p_company_info = new company_information;
<    } catch (bad_alloc xa) {
<       exit(COMPANY_ALLOC_ERR);
<    }
< 
106,112d100
<    try {
<       p_company_info->p_company_name = new char[strlen(company_name) + 1];
<    } catch (bad_alloc xa) {
<       exit(NAME_ALLOC_ERR);
<    }
< 
<    strcpy(p_company_info->p_company_name, company_name);
114c102
<    cin  >> p_company_info->number_of_employees;
---
>    cin  >> company_info.number_of_employees;
116c104
<    cin  >> p_company_info->bonus_year;
---
>    cin  >> company_info.bonus_year;
118c106
<    cin  >> p_company_info->bonus_amount;
---
>    cin  >> company_info.bonus_amount;
120c108
<    return *p_company_info;
---
>    return company_info;
123c111
< company_employee_bonus_record get_employees(company_information user_company)
---
> company_employee_bonus_record *get_employees(company_information user_company)
162c150
<    return *p_employee_db;
---
>    return p_employee_db;
184d171
< 

请注意,除了您想要的对象之外的所有堆分配对象(即company_employee_bonus_record *)现在都在堆栈上分配。这有助于清理事物,直到我可以专注于你的逻辑。

您的主要问题似乎是您尝试从调用new的函数返回分配在堆上的对象以分配存储。这不会奏效。正确的方法是从所述函数返回一个指针。

通过将company_employee_bonus_record结构保留在堆栈上,可以改进此设计。最简单的方法可能是将它们存储在矢量中。然而,如果你应用上述差异,你应该有一个工作程序。

祝你好运!