Thread: Weirdest error with '==' operator.

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Weirdest error with '==' operator.

    (WinXP, Borland C++ Free Command Line Compiler)
    Well, it came down to the wire and I guess I need an '==' operator, so I built one. Theirs probably some trick to do this, but maybe not. So, I just did it the only logical way I know;

    Code:
    class Platform {
     private:
      SHORT Strength;
      SHORT SteppedOn;
      SHORT x;
      SHORT y;
     public:
      Platform() {Strength=1; SteppedOn=0; x=0; y=0;}
      Platform(SHORT str, SHORT Step, SHORT x_pos, SHORT y_pos) {Strength=str; SteppedOn=Step; x=x_pos; y=y_pos;}
    
      void Set(SHORT str, SHORT Step, SHORT x_pos, SHORT y_pos) {Strength=str; SteppedOn=Step; x=x_pos; y=y_pos;}
    
      SHORT GetX() {return x;}
      SHORT GetY() {return y;}
      SHORT GetStr() {return Strength;}
      SHORT GetWStatus() {return SteppedOn;}
      SHORT operator ==(Platform q);
    };
    
    SHORT Platform::operator ==(Platform q) {
     if(q.GetX() == this.GetX() && q.GetY() == this.GetY() && q.GetStr() == this.GetStr() && q.GetWStatus() == this.GetWStatus)
      return 1;
    
     return 0;
    }
    Ok. That should work. But my compiler doesnt seem to like how I called my '.' structures.

    My errors:
    Code:
    C:\Borland\BCC55\Bin>BCC32 C:\MazeOfPain.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    C:\MazeOfPain.cpp:
    Error E2294 C:\PlatformClass.h 68: Structure required on left side of . or .* in function Platform::operator ==(Platform)
    Error E2294 C:\PlatformClass.h 68: Structure required on left side of . or .* in function Platform::operator ==(Platform)
    Error E2294 C:\PlatformClass.h 68: Structure required on left side of . or .* in function Platform::operator ==(Platform)
    Error E2294 C:\PlatformClass.h 68: Structure required on left side of . or .* in function Platform::operator ==(Platform)
    Warning W8057 C:\PlatformClass.h 72: Parameter 'q' is never used in function Platform::operator ==(Platform)
    *** 5 errors in Compile ***
    I'm not quite sure what this is asking? I tried calling my structure as if it was a pointer; "q->GetX()" it didint like that either. I'm quite confused... And why does it say 'q' is never used?

    Also, is their a better way to do this? I doubt big classes use this method, it'd get way to big.

    Anyways, why does my compiler hate me for this? -,-.

    Thank you!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    'this' is a pointer.
    try tis.
    *this
    or even the '->' operator.

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    class Platform {
     private:
      SHORT Strength;
      SHORT SteppedOn;
      SHORT x;
      SHORT y;
     public:
      Platform() {Strength=1; SteppedOn=0; x=0; y=0;}
      Platform(SHORT str, SHORT Step, SHORT x_pos, SHORT y_pos) {Strength=str; SteppedOn=Step; x=x_pos; y=y_pos;}
    
      void Set(SHORT str, SHORT Step, SHORT x_pos, SHORT y_pos) {Strength=str; SteppedOn=Step; x=x_pos; y=y_pos;}
    
      SHORT GetX() {return x;}
      SHORT GetY() {return y;}
      SHORT GetStr() {return Strength;}
      SHORT GetWStatus() {return SteppedOn;}
      SHORT operator==(Platform q);
      SHORT IsNull();
    };
    
    SHORT Platform::operator==(Platform q) {
     if(q.GetX() == this->GetX() && q.GetY() == this->GetY() && q.GetStr() == this->GetStr() && q.GetWStatus() == this->GetWStatus)
      return 1;
    
     return 0;
    }
    
    SHORT Platform::IsNull() {
     static Platform K(-1, 0, 0, 0);
    
     if(this == K)
      return 1;
    
     return 0;
    }
    Still gives me:

    Code:
    C:\Borland\BCC55\Bin>BCC32 c:\MazeOfPain.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    c:\MazeOfPain.cpp:
    Error E2235 c:\PlatformClass.h 68: Member function must be called or its address taken in function Platform::operator ==(Platform)
    Warning W8057 c:\PlatformClass.h 72: Parameter 'q' is never used in function Platform::operator ==(Platform)
    Error E2096 c:\PlatformClass.h 77: Illegal structure operation in function Platform::IsNull()
    *** 2 errors in Compile ***
    C:\Borland\BCC55\Bin>
    I still dont get the warnings either... And why is it still giving me an error when I use pointer dereferencing? And also, whats my problem with IsNull?

    Thank you!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    your method signature is wrong.
    should be
    Code:
    bool operator==(const Platform& q);

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    1) you forgot a pair of parenthesis: "q.GetWStatus() == this->GetWStatus" needs ()

    2) "if(this == K)" 'this' is a pointer to an object, not an object, so you need to dereference it: if(*this == K)

    hope this is correct :-/

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    SHORT Platform::operator==(Platform q)
    {
        if(q.GetX() == this->GetX() && q.GetY() == this->GetY() && q.GetStr() == this->GetStr() &&
           q.GetWStatus() == this->GetWStatus)
      return 1;
    
     return 0;
    }
    GetWStatus is a function, you need to use this->GetWStatus() and not this->GetWStatus.

    Code:
    SHORT Platform::IsNull() {
     static Platform K(-1, 0, 0, 0);
    
     if(this == K)
      return 1;
    
     return 0;
    }
    Again, you're forgetting that this is a pointer and not an object of type Platform. Coupled with how you have defined the operator (as a member function), you would need to say if( this->operator==(K) ). You should make your operators global friend functions as such:
    Code:
    class Platform
    {
       ...
       friend bool operator==(const Platform&, const Platform&);
    };
    
    bool operator==(const Platform& lhs, const Platform& rhs)
    {
        if(lhs.GetX() == rhs.GetX() && lhs.GetY() == rhs.GetY() && lhs.GetStr() == rhs.GetStr() &&
           lhs.GetWStatus() == rhs.GetWStatus())
            return true;
    
        else return false;
    }
    This will allow you to compare two Platform objects directly using == and say things like:
    Code:
    Platform foo(-1,0,0,0);
    Platform bar(-1,0,0,0);
    
    ...
    
    if( foo == bar )
    ...
    ... without needing the awkward syntax if( foo->operator==(bar) ) that wouild otherwise be required.


    This will also require you make all your "get" member functions const correct as they should have been all along:
    Code:
    class Platform {
       ...
        SHORT GetX() const {return x;}
        SHORT GetY() const {return y;}
        SHORT GetStr() const {return Strength;}
        SHORT GetWStatus() const {return SteppedOn;}
    };
    Last edited by hk_mp5kpdw; 02-07-2006 at 07:02 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    O wow! I'm a mega-noob to classes I guess :P. Well, it works! Thank you very much!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  3. Tic Tac Toe program...
    By Kross7 in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 03:25 PM
  4. Error message in tic tac toe problem please help
    By Kross7 in forum C++ Programming
    Replies: 17
    Last Post: 04-10-2007, 01:50 PM
  5. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 07:20 PM