2014-04-02 52 views
0

我遇到了將指針返回給一個對象數組的問題。輸入所有信息並順利進行後,當我嘗試僅使用指針打印出對象數組時,第一條記錄會正確打印出來,而下一條記錄只是混亂了數字。我認爲問題在於獲得公司員工的回報和任務。現在,我認爲只有第一個記錄纔會從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++; 
    } 
} 
+0

@brokenfoot對不起,我不認爲你明白我在問什麼。我沒有問題的結構,它的對象數組 –

+0

@brokenfoot我不認爲你的答案屬於我的問題。 'company_employee_bonus_record get_employees(company_information user_company)'是我遇到問題的函數 –

回答

1

有兩種解決問題的方法。

  1. 返回指向存儲在數據庫中的對象的指針。
  2. 從存儲在數據庫中的對象列表中返回一個對象。

爲了簡化這個答案,讓我們使用一個簡單的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(); 
} 
0

以下是從上面的貼版和一個工作版本之間運行的diff輸出:

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結構保留在堆棧上,可以改進此設計。最簡單的方法可能是將它們存儲在矢量中。然而,如果你應用上述差異,你應該有一個工作程序。

祝你好運!

相關問題