【问题标题】:Class Errors with Pointers指针类错误
【发布时间】:2013-10-01 19:14:27
【问题描述】:

我目前正在为一个程序编写一个类,这就是我想要完成的......

  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 - David Johnson”或类似“8:00 - 未分配”,如果 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
};

我收到多个错误,但不确定自己做错了什么?这是我第一次尝试用指针编写类,所以如果我的代码非常糟糕,我深表歉意。

【问题讨论】:

  • 您收到的错误是什么。请全部粘贴。
  • 第一件事,有什么错误?总是显示错误,您可以复制和粘贴尽可能多的输出!总是!

标签: c++ class pointers return void


【解决方案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),你必须像这样使用-&gt; 操作符:m_ptrEmployee-&gt;GetName()

运算符 cout 与 &lt;&lt; 运算符一起使用,而不是 &gt;&gt; 运算符。

我已经在你的代码中注释了你犯错的地方。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    相关资源
    最近更新 更多