faq; struct and class another difference?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fcvcnet

    #1

    faq; struct and class another difference?

    Hi,
    I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
    Jos¨¦e Lajoie, Barbara E. Moo
    "If we define a class using the class keyword, then any members
    defined before the first access label are implicitly private; ifwe
    usethe struct keyword, then those members are public. Whether we define
    a class using the class keyword or the struct keyword affects only the
    default initial access level."
    Now I defined a struct with overload operators == and != :
    ....
    typedef struct RecordTwoSegmen tNo
    {
    int firstsegmentno;
    int secondsegmentno ;
    bool operator == (const RTSN& rmyrtsn);
    bool operator != (const RTSN& rmyrtsn);
    }RTSN;

    bool RecordTwoSegmen tNo::operator ==(const RTSN &rmyrtsn)
    {
    return (firstsegmentno ==rmyrtsn.first segmentno &&
    secondsegmentno ==rmyrtsn.secon dsegmentno);

    }

    bool RecordTwoSegmen tNo::operator !=(const RTSN &rmyrtsn)
    {
    return !(*this==rmyrts n);
    }
    ....

    The compiler (vs2005+winxp) report:
    ------ Build started: Project: maxborder, Configuration: Debug Win32 ------
    Compiling...
    maxborder.cpp
    c:\maxborder\my point.h(16) : error C4430: missing type specifier - int
    assumed. Note: C++ does not support default-int
    c:\maxborder\my point.h(16) : error C2143: syntax error : missing ','
    before '&'
    c:\maxborder\my point.h(17) : error C4430: missing type specifier - int
    assumed. Note: C++ does not support default-int
    c:\maxborder\my point.h(17) : error C2143: syntax error : missing ','
    before '&'
    c:\maxborder\my point.h(21) : error C2511: 'bool
    RecordTwoSegmen tNo::operator ==(const RTSN &)' : overloaded member
    function not found in 'RecordTwoSegme ntNo'
    c:\maxborder\my point.h(13) : see declaration of 'RecordTwoSegme ntNo'
    c:\maxborder\my point.h(27) : error C2511: 'bool
    RecordTwoSegmen tNo::operator !=(const RTSN &)' : overloaded member
    function not found in 'RecordTwoSegme ntNo'
    c:\maxborder\my point.h(13) : see declaration of
    'RecordTwoSegme ntNo'
    ....
    What is wrong? Is struct and class has another difference?
    Thank you.

    --
    Thank you very much! :)
    Thank this newsgroup very much! :)
    Visual Studio 2005 Professional Edition
    Windows XP Professional
  • fcvcnet

    #2
    Re: faq; struct and class another difference?

    typedef struct RecordTwoSegmen tNo
    {
    int firstsegmentno;
    int secondsegmentno ;
    bool operator == (const RTSN& rmyrtsn);
    bool operator != (const RTSN& rmyrtsn);
    }RTSN;
    >
    ...
    What is wrong? Is struct and class has another difference?
    Thank you.
    >
    Sorry. I change it to
    struct RecordTwoSegmen tNo
    {
    int firstsegmentno;
    int secondsegmentno ;
    bool operator == (const RecordTwoSegmen tNo& rmyrtsn);
    bool operator != (const RecordTwoSegmen tNo& rmyrtsn);
    };

    typedef RecordTwoSegmen tNo RTSN;

    and it is solved.

    --
    Thank you very much! :)
    Thank this newsgroup very much! :)
    Visual Studio 2005 Professional Edition
    Windows XP Professional

    Comment

    • Rolf Magnus

      #3
      Re: faq; struct and class another difference?

      fcvcnet wrote:
      Hi,
      I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
      Josée Lajoie, Barbara E. Moo
      "If we define a class using the class keyword, then any members
      defined before the first access label are implicitly private; ifwe
      usethe struct keyword, then those members are public. Whether we define
      a class using the class keyword or the struct keyword affects only the
      default initial access level."
      It also affects the access level of inherited members of base classes for
      which it isn't explicitly specified.
      Now I defined a struct with overload operators == and != :
      ...
      typedef struct RecordTwoSegmen tNo
      Lose the typedef.
      {
      int firstsegmentno;
      int secondsegmentno ;
      bool operator == (const RTSN& rmyrtsn);
      bool operator != (const RTSN& rmyrtsn);
      }RTSN;
      >
      bool RecordTwoSegmen tNo::operator ==(const RTSN &rmyrtsn)
      {
      return (firstsegmentno ==rmyrtsn.first segmentno &&
      secondsegmentno ==rmyrtsn.secon dsegmentno);
      >
      }
      >
      bool RecordTwoSegmen tNo::operator !=(const RTSN &rmyrtsn)
      {
      return !(*this==rmyrts n);
      }
      ...
      >
      The compiler (vs2005+winxp) report:
      ------ Build started: Project: maxborder, Configuration: Debug Win32
      ------ Compiling...
      maxborder.cpp
      c:\maxborder\my point.h(16) : error C4430: missing type specifier - int
      assumed. Note: C++ does not support default-int
      c:\maxborder\my point.h(16) : error C2143: syntax error : missing ','
      before '&'
      c:\maxborder\my point.h(17) : error C4430: missing type specifier - int
      assumed. Note: C++ does not support default-int
      c:\maxborder\my point.h(17) : error C2143: syntax error : missing ','
      before '&'
      c:\maxborder\my point.h(21) : error C2511: 'bool
      RecordTwoSegmen tNo::operator ==(const RTSN &)' : overloaded member
      function not found in 'RecordTwoSegme ntNo'
      c:\maxborder\my point.h(13) : see declaration of
      'RecordTwoSegme ntNo'
      c:\maxborder\my point.h(27) : error C2511: 'bool
      RecordTwoSegmen tNo::operator !=(const RTSN &)' : overloaded member
      function not found in 'RecordTwoSegme ntNo'
      c:\maxborder\my point.h(13) : see declaration of
      'RecordTwoSegme ntNo'
      ...
      What is wrong? Is struct and class has another difference?
      No, it doesn't have anything to do with differences between class and
      struct. What makes you even believe that?
      The reason for your problem is that your typedef name is used inside your
      struct definition, but it's not known at that point yet.

      Comment

      • fcvcnet

        #4
        Re: faq; struct and class another difference?

        Rolf Magnus 写道:
        No, it doesn't have anything to do with differences between class and
        struct. What makes you even believe that?
        The reason for your problem is that your typedef name is used inside your
        struct definition, but it's not known at that point yet.
        >
        Thank you very much. Now I know where I am wrong.

        --
        Thank you very much! :)
        Thank this newsgroup very much! :)
        Visual Studio 2005 Professional Edition
        Windows XP Professional

        Comment

        • Salt_Peter

          #5
          Re: faq; struct and class another difference?

          On Mar 31, 11:57 am, fcvcnet <fcvc...@163.co mwrote:
          Hi,
          I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
          Jos¨¦e Lajoie, Barbara E. Moo
          "If we define a class using the class keyword, then any members
          defined before the first access label are implicitly private; ifwe
          usethe struct keyword, then those members are public. Whether we define
          a class using the class keyword or the struct keyword affects only the
          default initial access level."
          Now I defined a struct with overload operators == and != :
          ...
          typedef struct RecordTwoSegmen tNo
          {
          int firstsegmentno;
          int secondsegmentno ;
          bool operator == (const RTSN& rmyrtsn);
          bool operator != (const RTSN& rmyrtsn);
          the type RTSN is not declared yet.
          This is clearly explained in the errors you see below.
          >
          }RTSN;
          >
          bool RecordTwoSegmen tNo::operator ==(const RTSN &rmyrtsn)
          {
          return (firstsegmentno ==rmyrtsn.first segmentno &&
          secondsegmentno ==rmyrtsn.secon dsegmentno);
          >
          }
          >
          bool RecordTwoSegmen tNo::operator !=(const RTSN &rmyrtsn)
          {
          return !(*this==rmyrts n);}
          >
          ...
          >
          The compiler (vs2005+winxp) report:
          ------ Build started: Project: maxborder, Configuration: Debug Win32 ------
          Compiling...
          maxborder.cpp
          c:\maxborder\my point.h(16) : error C4430: missing type specifier - int
          assumed. Note: C++ does not support default-int
          c:\maxborder\my point.h(16) : error C2143: syntax error : missing ','
          before '&'
          c:\maxborder\my point.h(17) : error C4430: missing type specifier - int
          assumed. Note: C++ does not support default-int
          c:\maxborder\my point.h(17) : error C2143: syntax error : missing ','
          before '&'
          c:\maxborder\my point.h(21) : error C2511: 'bool
          RecordTwoSegmen tNo::operator ==(const RTSN &)' : overloaded member
          function not found in 'RecordTwoSegme ntNo'
          c:\maxborder\my point.h(13) : see declaration of 'RecordTwoSegme ntNo'
          c:\maxborder\my point.h(27) : error C2511: 'bool
          RecordTwoSegmen tNo::operator !=(const RTSN &)' : overloaded member
          function not found in 'RecordTwoSegme ntNo'
          c:\maxborder\my point.h(13) : see declaration of
          'RecordTwoSegme ntNo'
          ...
          What is wrong? Is struct and class has another difference?
          Thank you.
          >
          --
          Other than default access, there is no difference whatsoever between
          struct and class.
          The exception to that rule is a POD.
          If you need to declare a class and then typedefine it do it on a
          seperate line or don't do it at all. The reason for that is obvious
          from your code above.

          #include <iostream>

          struct Record
          {
          Record(int f = 0, int s = 0) : first(f), second(s) { }
          bool operator == (const Record& record) const;
          bool operator != (const Record& record) const;
          private:
          int first;
          int second;
          };

          // typedef Record RTSN;

          bool Record::operato r == (const Record& record) const
          {
          return ( first == record.first &&
          second == record.second );
          }

          bool Record::operato r != (const Record& record) const
          {
          return !(*this == record);
          }

          int main()
          {
          Record a, b; // default intialized
          Record c(1,1);

          std::cout << "a == b: " << (a == b) << std::endl;
          std::cout << "a == c: " << (a == c) << std::endl;
          std::cout << "a != b: " << (a != b) << std::endl;
          std::cout << "a != c: " << (a != c) << std::endl;
          }

          /*
          a == b: 1
          a == c: 0
          a != b: 0
          a != c: 1
          */



          Comment

          • James Kanze

            #6
            Re: faq; struct and class another difference?

            On Mar 31, 7:01 pm, "Salt_Peter " <pj_h...@yahoo. comwrote:
            On Mar 31, 11:57 am, fcvcnet <fcvc...@163.co mwrote:
            [...]
            Other than default access, there is no difference whatsoever between
            struct and class.
            The exception to that rule is a POD.
            In what way? Both:

            struct Toto { int x; int y; } ;
            and
            class Titi { public : int x; int y ; } ;

            are PODS. For the compiler, it makes no difference. (For the
            human reader, of course, one generally expects the former.)

            --
            James Kanze (Gabi Software) email: james.kanze@gma il.com
            Conseils en informatique orient¨¦e objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place S¨¦mard, 78210 St.-Cyr-l'¨¦cole, France, +33 (0)1 30 23 00 34

            Comment

            • Old Wolf

              #7
              Re: faq; struct and class another difference?

              On Apr 1, 10:13 pm, "James Kanze" <james.ka...@gm ail.comwrote:
              On Mar 31, 7:01 pm, "Salt_Peter " <pj_h...@yahoo. comwrote:
              >
              Other than default access, there is no difference whatsoever between
              struct and class. The exception to that rule is a POD.
              >
              In what way? Both:
              >
              struct Toto { int x; int y; } ;
              and
              class Titi { public : int x; int y ; } ;
              >
              are PODS. For the compiler, it makes no difference.
              There is another difference: the default derivation type
              for structs is 'public', but for classes it is 'private'.


              Comment

              Working...