Scope-problems

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

    Scope-problems

    Hi

    I'm having some problems with a class for large Integers (called
    Xuint).

    I want (have) to implement it like this:
    - using a simple linked list
    - a reference-objekt for each value, which means if 2 Xuint-objekts
    have the same value, only one list is in memory and the 2
    Xuint-objekts are pointing to the ref-objekt which points to the list.

    By now I have this:

    class Xuint
    {
    //listnode
    struct Xunode{
    unsigned short nodeval;
    Xunode *next;
    };

    //ref-objekt
    class Rep{
    friend class Xuint;
    Xunode *xustart;
    int refs;
    //...
    };

    public:
    //...
    };

    When I compile this with Forte Developer C++ 5.4 I get:
    "Error: Xuint::Xunode is not accessible from Xuint::Rep"

    If I make Xunode public it works, but I don't understand why, because
    from my point of view the 'friend class Xuint;' should make the
    private parts of Xuint accessible to Rep.

    So here's the question: what am I doing wrong and is there a way to
    keep Xunode private while using it from Rep?

    regards,
    Michael
  • Victor Bazarov

    #2
    Re: Scope-problems

    "Michael S." <eyesshuttight@ web.de> wrote...[color=blue]
    > I'm having some problems with a class for large Integers (called
    > Xuint).
    >
    > I want (have) to implement it like this:
    > - using a simple linked list
    > - a reference-objekt for each value, which means if 2 Xuint-objekts
    > have the same value, only one list is in memory and the 2
    > Xuint-objekts are pointing to the ref-objekt which points to the list.
    >
    > By now I have this:
    >
    > class Xuint
    > {
    > //listnode
    > struct Xunode{
    > unsigned short nodeval;
    > Xunode *next;
    > };
    >
    > //ref-objekt
    > class Rep{
    > friend class Xuint;
    > Xunode *xustart;
    > int refs;
    > //...
    > };
    >
    > public:
    > //...
    > };
    >
    > When I compile this with Forte Developer C++ 5.4 I get:
    > "Error: Xuint::Xunode is not accessible from Xuint::Rep"
    >
    > If I make Xunode public it works, but I don't understand why, because
    > from my point of view the 'friend class Xuint;' should make the
    > private parts of Xuint accessible to Rep.
    >
    > So here's the question: what am I doing wrong and is there a way to
    > keep Xunode private while using it from Rep?[/color]

    I think you got the friendship wrong. Only a friend of class A
    can access A's private members. If you write a class B and then
    declare that A is a friend of B, it does NOT make B a friend of
    A automatically.

    In your case, in order for 'Rep' to access 'Xunode', 'Rep' has
    to be declared a friend of 'Xuint'.

    Victor


    Comment

    • Wagner Bruna

      #3
      Re: Scope-problems

      Hi,

      eyesshuttight@w eb.de (Michael S.) wrote in message news:<b5aee4d3. 0312291335.293c 0da4@posting.go ogle.com>...[color=blue]
      > Hi
      >
      > I'm having some problems with a class for large Integers (called
      > Xuint).
      >
      > I want (have) to implement it like this:
      > - using a simple linked list
      > - a reference-objekt for each value, which means if 2 Xuint-objekts
      > have the same value, only one list is in memory and the 2
      > Xuint-objekts are pointing to the ref-objekt which points to the list.
      >
      > By now I have this:
      >
      > class Xuint
      > {
      > //listnode
      > struct Xunode{
      > unsigned short nodeval;
      > Xunode *next;
      > };
      >
      > //ref-objekt
      > class Rep{
      > friend class Xuint;
      > Xunode *xustart;
      > int refs;
      > //...
      > };
      >
      > public:
      > //...
      > };
      >
      > When I compile this with Forte Developer C++ 5.4 I get:
      > "Error: Xuint::Xunode is not accessible from Xuint::Rep"
      >
      > If I make Xunode public it works, but I don't understand why, because
      > from my point of view the 'friend class Xuint;' should make the
      > private parts of Xuint accessible to Rep.[/color]

      Yes, it should; I tested your code on Comeau online and gcc 3.2.3, and
      it works fine.
      [color=blue]
      > So here's the question: what am I doing wrong and is there a way to
      > keep Xunode private while using it from Rep?[/color]

      Hard to say, since it's compiler's fault... But you could use a
      forward declaration to at least keep the implementation hidden:

      class Xuint
      {
      public:
      //listnode
      struct Xunode;
      private:
      //ref-objekt
      class Rep{
      friend class Xuint;
      Xunode *xustart;
      int refs;
      //...
      };

      public:
      //...
      };

      // (maybe) Xuint.cpp

      struct Xuint::Xunode
      {
      unsigned short nodeval;
      Xunode *next;
      };

      Or maybe you could declare Xunode inside Rep.

      --
      Wagner

      Comment

      • Jeff Schwab

        #4
        Re: Scope-problems

        Wagner Bruna wrote:[color=blue]
        > Hi,
        >
        > eyesshuttight@w eb.de (Michael S.) wrote in message news:<b5aee4d3. 0312291335.293c 0da4@posting.go ogle.com>...
        >[color=green]
        >>Hi
        >>
        >>I'm having some problems with a class for large Integers (called
        >>Xuint).
        >>
        >>I want (have) to implement it like this:
        >>- using a simple linked list
        >>- a reference-objekt for each value, which means if 2 Xuint-objekts
        >>have the same value, only one list is in memory and the 2
        >>Xuint-objekts are pointing to the ref-objekt which points to the list.
        >>
        >>By now I have this:
        >>
        >>class Xuint
        >>{
        >> //listnode
        >> struct Xunode{
        >> unsigned short nodeval;
        >> Xunode *next;
        >> };
        >>
        >> //ref-objekt
        >> class Rep{
        >> friend class Xuint;
        >> Xunode *xustart;
        >> int refs;
        >> //...
        >> };
        >>
        >> public:
        >> //...
        >>};
        >>
        >>When I compile this with Forte Developer C++ 5.4 I get:
        >>"Error: Xuint::Xunode is not accessible from Xuint::Rep"
        >>
        >>If I make Xunode public it works, but I don't understand why, because
        >>from my point of view the 'friend class Xuint;' should make the
        >>private parts of Xuint accessible to Rep.[/color]
        >
        >
        > Yes, it should; I tested your code on Comeau online and gcc 3.2.3, and
        > it works fine.[/color]

        I believe this issue is covered by DR 45:



        Since Rep is a member of Xuint, it ought to have access to all the other
        members (including Xunode). No friend declarations should be needed.
        However, I believe Forte 5.4 is in compliance with the existing
        standard. Do you have access to a newer version of the compiler?

        -Jeff

        Comment

        • Michael S.

          #5
          Re: Scope-problems

          "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:<Gl3Ib.696 801$Fm2.600116@ attbi_s04>...[color=blue]
          > "Michael S." <eyesshuttight@ web.de> wrote...[color=green]
          > > I'm having some problems with a class for large Integers (called
          > > Xuint).
          > >
          > > I want (have) to implement it like this:
          > > - using a simple linked list
          > > - a reference-objekt for each value, which means if 2 Xuint-objekts
          > > have the same value, only one list is in memory and the 2
          > > Xuint-objekts are pointing to the ref-objekt which points to the list.
          > >
          > > By now I have this:
          > >
          > > class Xuint
          > > {
          > > //listnode
          > > struct Xunode{
          > > unsigned short nodeval;
          > > Xunode *next;
          > > };
          > >
          > > //ref-objekt
          > > class Rep{
          > > friend class Xuint;
          > > Xunode *xustart;
          > > int refs;
          > > //...
          > > };
          > >
          > > public:
          > > //...
          > > };
          > >
          > > When I compile this with Forte Developer C++ 5.4 I get:
          > > "Error: Xuint::Xunode is not accessible from Xuint::Rep"
          > >
          > > If I make Xunode public it works, but I don't understand why, because
          > > from my point of view the 'friend class Xuint;' should make the
          > > private parts of Xuint accessible to Rep.
          > >
          > > So here's the question: what am I doing wrong and is there a way to
          > > keep Xunode private while using it from Rep?[/color]
          >
          > I think you got the friendship wrong. Only a friend of class A
          > can access A's private members. If you write a class B and then
          > declare that A is a friend of B, it does NOT make B a friend of
          > A automatically.
          >
          > In your case, in order for 'Rep' to access 'Xunode', 'Rep' has
          > to be declared a friend of 'Xuint'.
          >
          > Victor[/color]

          You're right I really got that wrong, but when I insert a 'friend
          class Rep;' into class Xuint I still get the same error.

          Michael

          Comment

          • Michael S.

            #6
            Re: Scope-problems

            Jeff Schwab <jeffplus@comca st.net> wrote in message news:<U62dnRGmA f-Zb22iRVn-gw@comcast.com> ...[color=blue]
            > Wagner Bruna wrote:[color=green]
            > > Hi,
            > >
            > > eyesshuttight@w eb.de (Michael S.) wrote in message news:<b5aee4d3. 0312291335.293c 0da4@posting.go ogle.com>...
            > >[color=darkred]
            > >>Hi
            > >>
            > >>I'm having some problems with a class for large Integers (called
            > >>Xuint).
            > >>
            > >>I want (have) to implement it like this:
            > >>- using a simple linked list
            > >>- a reference-objekt for each value, which means if 2 Xuint-objekts
            > >>have the same value, only one list is in memory and the 2
            > >>Xuint-objekts are pointing to the ref-objekt which points to the list.
            > >>
            > >>By now I have this:
            > >>
            > >>class Xuint
            > >>{
            > >> //listnode
            > >> struct Xunode{
            > >> unsigned short nodeval;
            > >> Xunode *next;
            > >> };
            > >>
            > >> //ref-objekt
            > >> class Rep{
            > >> friend class Xuint;
            > >> Xunode *xustart;
            > >> int refs;
            > >> //...
            > >> };
            > >>
            > >> public:
            > >> //...
            > >>};
            > >>
            > >>When I compile this with Forte Developer C++ 5.4 I get:
            > >>"Error: Xuint::Xunode is not accessible from Xuint::Rep"
            > >>
            > >>If I make Xunode public it works, but I don't understand why, because
            > >>from my point of view the 'friend class Xuint;' should make the
            > >>private parts of Xuint accessible to Rep.[/color]
            > >
            > >
            > > Yes, it should; I tested your code on Comeau online and gcc 3.2.3, and
            > > it works fine.[/color]
            >
            > I believe this issue is covered by DR 45:
            >
            > http://anubis.dkuug.dk/jtc1/sc22/wg2...efects.html#45
            >
            > Since Rep is a member of Xuint, it ought to have access to all the other
            > members (including Xunode). No friend declarations should be needed.
            > However, I believe Forte 5.4 is in compliance with the existing
            > standard. Do you have access to a newer version of the compiler?
            >
            > -Jeff[/color]
            No, I have to use that specific Compiler, because our prof insists on
            that one.

            I thank all of you for your comments, but I think it will do little
            harm if I make Xunode public, although I think from a stylistic point
            of view it should be private.

            regards,
            Michael

            Comment

            • red floyd

              #7
              Re: Scope-problems

              Michael S. wrote:[color=blue]
              > Hi
              >
              > I'm having some problems with a class for large Integers (called
              > Xuint).
              >
              > I want (have) to implement it like this:
              > - using a simple linked list
              > - a reference-objekt for each value, which means if 2 Xuint-objekts
              > have the same value, only one list is in memory and the 2
              > Xuint-objekts are pointing to the ref-objekt which points to the list.
              >
              > By now I have this:
              >
              > class Xuint
              > {
              > //listnode
              > struct Xunode{
              > unsigned short nodeval;
              > Xunode *next;
              > };
              >
              > //ref-objekt
              > class Rep{
              > friend class Xuint;
              > Xunode *xustart;
              > int refs;
              > //...
              > };
              >
              > public:
              > //...
              > };
              >
              > When I compile this with Forte Developer C++ 5.4 I get:
              > "Error: Xuint::Xunode is not accessible from Xuint::Rep"
              >
              > If I make Xunode public it works, but I don't understand why, because
              > from my point of view the 'friend class Xuint;' should make the
              > private parts of Xuint accessible to Rep.[/color]

              Youve got it backwards. The friend declaration makes the private parts of Xuint::Rep available to Xuint.
              What you need in Xuint is also a friend declaration:

              friend class Xuint::Rep;

              Because Xunode is private within Xuint, and Xuint::Rep has no special priviliges regarding Xuint, it can't see Xunode.

              Comment

              Working...