Scope-problems

M

Michael S.

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
 
V

Victor Bazarov

Michael S. said:
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?

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
 
W

Wagner Bruna

Hi,

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.

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

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.
 
J

Jeff Schwab

Wagner said:
Hi,




Yes, it should; I tested your code on Comeau online and gcc 3.2.3, and
it works fine.

I believe this issue is covered by DR 45:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.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
 
M

Michael S.

Victor Bazarov said:
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

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
 
M

Michael S.

Jeff Schwab said:
I believe this issue is covered by DR 45:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.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
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
 
R

red floyd

Michael said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,177
Messages
2,570,954
Members
47,507
Latest member
codeguru31

Latest Threads

Top