2013-09-26 40 views
0

我目前正在寫一類的節目,這就是我試圖完成......類錯誤與指針

  1. 設置:設置m_ptrEmployee爲NULL,m_beginHour到小時。

  2. AssignEmployee:將m_ptrEmployee設置爲員工。

  3. GetEmployeeName:使用m_ptrEmployeeEmployee.GetName返回員工 名。如果m_ptrEmployee爲NULL,則返回「UNALLOCATED」。

  4. 輸出:利用m_ptrEmployeeEmployee.GetName顯示m_beginHour和 員工的名字是這樣的,「8:00大衛 - 約翰遜」或類似這樣的,「8:00 - UNALLOCATED」,如果m_ptrEmployee爲NULL。

  5. 重置:將m_ptrEmployee重置爲NULL。

  6. GetIsSet:如果m_ptrEmployee不爲NULL,則返回true,否則返回false。

這裏是我的代碼...

#include <string> 
using namespace std; 

#include "Employee.h" 

class Schedule 
{ 
    public: 
    void Setup(int hour) 
     { 
      m_ptrEmployee = NULL; 
      m_beginHour = hour; 
     }; 
    void AssignEmployee(Employee* employee) 
     { 
      m_ptrEmployee = employee; 
     }; 
    string GetEmployeeName() 
     { 
      if (m_ptrEmployee = NULL) 
       return "UNALLOCATED" 
      else 
       return Employee.GetName() 
     }; 
    void Output() 
     { 
      if (m_ptrEmployee = NULL) 
       cout>> m_beginHour>>"--">>"UNALLOCATED">>endl; 
      else 
       cout>>m_beginHour>>"--">>GetName()>>endl; 
     } 
    void Reset() 
     { 
      m_ptrEmployee = NULL; 
     } 
    bool GetIsSet() 
     { 
      if (m_ptrEmployee != NULL) 
       return true; 
      else 
       return false; 
     } 
    private: 
    Employee* m_ptrEmployee; 
    int m_beginHour; 
}; 

GetName()包括在之前的類,它是...

public: 
void Setup(const string& first, const string& last, float pay); 
{ 
    m_firstName = first; 
    m_lastName = last; 
    m_payPerHour = pay; 
    m_activeEmployee = true; 
} 

string GetName() 
{ 
    return m_firstName+""+m_lastName 
}; 

我收到多個錯誤和我米不知道我做錯了什麼?這是我第一次嘗試用指針編寫類,所以我很抱歉如果我的代碼是絕對可怕的。

+0

您收到了哪些錯誤。請將它們全部粘貼。 – 2013-09-26 01:14:37

+1

首先,錯誤是什麼?總是顯示錯誤,您可以隨意複製和粘貼儘可能多的輸出!總是! –

回答

1

這裏是一些更正:

一般來說,要小心比較在C++中。比較兩件事情時,不能使用直觀的=。您必須使用==。如果您使用=,則會導致分配而不是測試。另外,在聲明結束時不要忘記您的分號;

不好比較:

if (m_ptrEmployee = NULL) //Assigns NULL to m_ptrEmployee and then tests m_ptrEmployee 
          //Always false because m_ptrEmployee was just assigned NULL 

很好的比較:

if (m_ptrEmployee == NULL) //Better. This is false only when m_ptrEmployee equals NULL 

當你想通過指針(如m_ptrEmployee),您必須使用->運營商訪問類的成員像這樣:m_ptrEmployee->GetName()

操作員cout與<<操作員一起使用,而不是>>操作員rator。

我註釋了代碼中出現錯誤的位置。

#include <string> 
using namespace std; 

#include "Employee.h" 

class Schedule 
{ 
    public: 
    void Setup(int hour) 
     { 
      m_ptrEmployee = NULL; 
      m_beginHour = hour; 
     }; 
    void AssignEmployee(Employee* employee) 
     { 
      m_ptrEmployee = employee; 
     }; 
    string GetEmployeeName() 
     { 
      if (m_ptrEmployee == NULL) //Comparison always takes double == 
       return "UNALLOCATED"; 
      else 
       return m_ptrEmployee->GetName(); //Use employee pointer with -> operator 
     }; 
    void Output() 
     { 
      if (m_ptrEmployee == NULL) //Careful with comparisons. Always use ==, not = 
       cout << m_beginHour << "--" << "UNALLOCATED" << endl; //Operator << was the other way around. It's not >>, but << for cout 
      else 
       cout << m_beginHour << "--" << m_ptrEmployee->GetName() << endl; 
     } 
    void Reset() 
     { 
      m_ptrEmployee = NULL; 
     } 
    bool GetIsSet() 
     { 
      if (m_ptrEmployee != NULL) 
       return true; 
      else 
       return false; 
     } 

    private: 
    Employee* m_ptrEmployee; 
    int m_beginHour; 
};