0

I'm currently writing a class for a program and this is what I'm attempting to accomplish...

  1. Setup: Sets m_ptrEmployee to NULL and m_beginHour to hour.

  2. AssignEmployee: Sets m_ptrEmployee to employee.

  3. GetEmployeeName: Uses m_ptrEmployee and Employee.GetName to return the employees name. Returns “UNALLOCATED”, if m_ptrEmployee is NULL.

  4. Output: Uses m_ptrEmployee and Employee.GetName to display m_beginHour and the employee’s name something like this, “8:00 - David Johnson” or like this, “8:00 - UNALLOCATED”, if m_ptrEmployee is NULL.

  5. Reset: Resets m_ptrEmployee to NULL.

  6. GetIsSet: Returns true, if m_ptrEmployee is not NULL and false otherwise.

Here is my code...

#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() is included in a previous class and it is...

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
};

I'm receiving multiple errors and I'm not sure what I'm doing wrong? This is my first time attempting to write classes with pointers, so I apologize if my code is absolutely awful.

arserbin3
  • 6,010
  • 8
  • 36
  • 52
Bob
  • 1,344
  • 3
  • 29
  • 63
  • What are the errors you are receiving. Please paste them all. –  Sep 26 '13 at 01:14
  • 1
    FIRST THINGS FIRST, what are the errors? ALWAYS show the errors, you copy and paste as much output as you like! ALWAYS! – Alec Teal Sep 26 '13 at 01:14

1 Answers1

1

Here are some corrections:

In general, be careful with comparisons in C++. You can't use the intuitive = when comparing two things. You have to use ==. If you use =, it results in an assignment and not a test. Also, do not forget your semicolon ; at the end of a statement.

Bad comparison:

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

Good comparison:

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

When you want to access a member of a class through a pointer (such as m_ptrEmployee), you have to use the -> operator like so: m_ptrEmployee->GetName()

Operator cout is used with the << operator, and not the >> operator.

I have annotated the places in your code where you made mistakes.

#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;
};