Help...Operator Overloading...Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • titan0111@hotmail.com

    #1

    Help...Operator Overloading...Problem

    ----inside of class called snowfall, i wrote----

    int operator == (char[]);


    ----and right before main() i wrote----

    int snowfall::opera tor == (char str[])

    // comparing two member data (dates)

    {
    return (strcmp(date, str) == 0) ? 1 : 0;
    }


    ----inside of main i called it----

    while (sf[i].getdate() != "00/00")
    {...}

    i was told that this won't work. can you tell me what's wrong?
    and give me the correct way to write it?

  • Ron Natalie

    #2
    Re: Help...Operator Overloading...P roblem

    titan0111@hotma il.com wrote:[color=blue]
    > ----inside of class called snowfall, i wrote----
    >
    > int operator == (char[]);
    >
    >[/color]
    [color=blue]
    > while (sf[i].getdate() != "00/00")
    > {...}
    >
    > i was told that this won't work. can you tell me what's wrong?
    > and give me the correct way to write it?
    >[/color]
    == and != are two distinct operators. You defined one but
    are using the other.

    Comment

    • Victor Bazarov

      #3
      Re: Help...Operator Overloading...P roblem

      titan0111@hotma il.com wrote:[color=blue]
      > ----inside of class called snowfall, i wrote----
      >
      > int operator == (char[]);[/color]

      Make it

      bool operator==(cons t char*) const;
      [color=blue]
      >
      >
      > ----and right before main() i wrote----
      >
      > int snowfall::opera tor == (char str[])[/color]

      bool snowfall::opera tor == (char const *str) const
      [color=blue]
      >
      > // comparing two member data (dates)
      >
      > {
      > return (strcmp(date, str) == 0) ? 1 : 0;[/color]

      return strcmp(date, str) == 0;
      [color=blue]
      > }
      >
      >
      > ----inside of main i called it----
      >
      > while (sf[i].getdate() != "00/00")
      > {...}
      >
      > i was told that this won't work. can you tell me what's wrong?[/color]

      Not with much assuredness lacking the definition of 'snowfall' type
      and the declaration of 'sf' here. But I suspect the idea is to do

      while (sf[i] != "00/00")
      [color=blue]
      > and give me the correct way to write it?[/color]

      See above. And next time read the FAQ.

      V

      Comment

      • Victor Bazarov

        #4
        Re: Help...Operator Overloading...P roblem

        Victor Bazarov wrote:[color=blue]
        > titan0111@hotma il.com wrote:[/color]
        [...][color=blue][color=green]
        >> ----inside of main i called it----
        >>
        >> while (sf[i].getdate() != "00/00")
        >> {...}
        >>
        >> i was told that this won't work. can you tell me what's wrong?[/color]
        >
        >
        > Not with much assuredness lacking the definition of 'snowfall' type
        > and the declaration of 'sf' here. But I suspect the idea is to do
        >
        > while (sf[i] != "00/00")[/color]

        And, yes, I missed the fact that you're using != having defined ==.
        Ron is right. Pay attention what operator you're using.

        V

        Comment

        • titan0111@hotmail.com

          #5
          Re: Help...Operator Overloading...P roblem

          ---in class i changed to----

          int operator != (char[]);

          ---right after class i changed difinition to----

          int snowfall::opera tor != (char str[])

          // overloading operator = to compare two strings

          {
          return (strcmp(date, str) == 0) ? 0 : 1;
          }

          ----in main() i wrote----
          while (sf[i].getdate() != "00/00")

          and it won't still work....

          Comment

          • Victor Bazarov

            #6
            Re: Help...Operator Overloading...P roblem

            titan0111@hotma il.com wrote:[color=blue]
            > ---in class i changed to----
            >
            > int operator != (char[]);
            >
            > ---right after class i changed difinition to----
            >
            > int snowfall::opera tor != (char str[])
            >
            > // overloading operator = to compare two strings
            >
            > {
            > return (strcmp(date, str) == 0) ? 0 : 1;
            > }
            >
            > ----in main() i wrote----
            > while (sf[i].getdate() != "00/00")
            >
            > and it won't still work....
            >[/color]

            Read the FAQ 5.8 (http://www.parashift.com/c++-faq-lite/)

            V

            Comment

            • Karl Heinz Buchegger

              #7
              Re: Help...Operator Overloading...P roblem

              titan0111@hotma il.com wrote:[color=blue]
              >
              > ---in class i changed to----
              >
              > int operator != (char[]);
              >
              > ---right after class i changed difinition to----
              >
              > int snowfall::opera tor != (char str[])
              >
              > // overloading operator = to compare two strings
              >
              > {
              > return (strcmp(date, str) == 0) ? 0 : 1;
              > }
              >
              > ----in main() i wrote----
              > while (sf[i].getdate() != "00/00")
              >
              > and it won't still work....[/color]

              Then please tell us what 'does not work' means!
              Do you get an error message from the compiler? If yes,
              which one?

              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              Working...