in
Welcome to Lionhead Community Sign in to Windows Live ID | Help

Problem with Overloaded Equality Operator

Last post 11-21-2009, 18:29 by XALAN. 2 replies.
Sort Posts: Previous Next
  •  11-21-2009, 14:56 3418927

    Problem with Overloaded Equality Operator

    As you can tell from the title I'm trying to build an overloaded equality operator to see when two objects are equal, however it doesn't seem to work, so I was hoping one of you fine people could point out the problem to me.

    here's the code, not: m_speed, m_altitude, m_bearing, m_start are integers; m_x and m_y are floats; and m_name is a character array.

    bool FlightPlan:Surprise [:O]perator==(const FlightPlan &other)
    {
    fprintf (stderr, "Equality Operator runs to #1\n");
    if (other.m_speed==m_speed && other.m_bearing==m_bearing && other.m_altitude==m_altitude && other.m_start==m_start && other.m_x==m_x && other.m_y==m_y)
    {//control#1: to check Flight I.D.
    fprintf (stderr, "Equality Operator runs to #2\n");
    if (strlen(m_name)!=strlen(other.m_name) || m_name==0 || other.m_name==0)//if not the same length or either is NULL, not equal
    return false;
    fprintf (stderr, "Equality Operator runs to #3\n");
    for (int i=0; strlen(m_name); i++)
    if (m_name[i]!=other.m_name[i])//if unique characters exist, not equal
    return false;
    fprintf (stderr, "Equality Operator runs to #4\n");
    return true;//if it passes all other tests then objects, equal
    }//end control#1
    else
    return false;
    }

    Here's the relevant stderr output:
    Equality Operator runs to #1

    The program has unexpectedly finished.

    Thanks,
    James
  •  11-21-2009, 18:18 3418936 in reply to 3418927

    Re: Problem with Overloaded Equality Operator

    I wrote up a quick console app and tested the code. I think the only thing that I had to fix was your "for loop" where you had


    for (int i=0; strlen(m_name) ; i++ )


    and I changed it to:


    for (unsigned int i=0; i


    Here's my full code:

    class FlightPlan{
    public:

    int m_speed, m_altitude, m_bearing, m_start;
    float m_x, m_y;
    char* m_name;

    FlightPlan(int speed, int altitude, int bearing, int start, float x, float y, char* name) : m_speed(speed), m_altitude(altitude), m_bearing(bearing), m_start(start), m_x(x), m_y(y), m_name(name) {}

    bool operator==(const FlightPlan &other)
    {
    fprintf (stderr, "Equality Operator runs to #1\n");
    if (other.m_speed==m_speed && other.m_bearing==m_bearing && other.m_altitude==m_altitude && other.m_start==m_start && other.m_x==m_x && other.m_y==m_y)
    {//control#1: to check Flight I.D.
    fprintf (stderr, "Equality Operator runs to #2\n");
    if (strlen(m_name)!=strlen(other.m_name) || m_name==0 || other.m_name==0)//if not the same length or either is NULL, not equal
    {
    return false;
    }
    fprintf (stderr, "Equality Operator runs to #3\n");
    for (unsigned int i=0; i {
    if (m_name[i]!=other.m_name[i])
    {//if unique characters exist, not equal
    return false;
    }
    }
    fprintf (stderr, "Equality Operator runs to #4\n");
    return true;//if it passes all other tests then objects, equal
    }//end control#1
    else
    return false;
    }
    };



    int _tmain(int argc, _TCHAR* argv[])
    {
    FlightPlan flightPlan1(0, 0, 0, 0, 1.0, 1.0, "Flight Plan 1");

    FlightPlan flightPlan2(0, 0, 0, 0, 1.0, 1.0, "Flight Plan 1");

    flightPlan1 == flightPlan2;



    return 0;
    }
    http://projectthwart.sourceforge.net/
  •  11-21-2009, 18:29 3418938 in reply to 3418936

    Re: Problem with Overloaded Equality Operator

    It appears that posting code doesn't work too well on this site. I think I'm using the developer version of this forum editor too.

    So the code won't compile above until you fix it. We gotta figure out how others are posting code on this forum.
    http://projectthwart.sourceforge.net/
View as RSS news feed in XML