 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael S. Guest
|
Posted: Mon Dec 29, 2003 9:35 pm Post subject: 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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Dec 30, 2003 12:08 am Post subject: Re: Scope-problems |
|
|
"Michael S." <eyesshuttight (AT) web (DOT) de> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
Wagner Bruna Guest
|
Posted: Tue Dec 30, 2003 2:30 am Post subject: Re: Scope-problems |
|
|
Hi,
[email]eyesshuttight (AT) web (DOT) de[/email] (Michael S.) wrote in message news:<b5aee4d3.0312291335.293c0da4 (AT) posting (DOT) google.com>...
| Quote: | 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.
| Quote: | 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.
--
Wagner
|
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Tue Dec 30, 2003 3:26 am Post subject: Re: Scope-problems |
|
|
Wagner Bruna wrote:
| Quote: | Hi,
[email]eyesshuttight (AT) web (DOT) de[/email] (Michael S.) wrote in message news:<b5aee4d3.0312291335.293c0da4 (AT) posting (DOT) google.com>...
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.
|
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
|
|
| Back to top |
|
 |
Michael S. Guest
|
Posted: Tue Dec 30, 2003 6:52 pm Post subject: Re: Scope-problems |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | "Michael S." <eyesshuttight (AT) web (DOT) de> wrote...
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
|
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
|
|
| Back to top |
|
 |
Michael S. Guest
|
Posted: Tue Dec 30, 2003 7:02 pm Post subject: Re: Scope-problems |
|
|
Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote
| Quote: | Wagner Bruna wrote:
Hi,
[email]eyesshuttight (AT) web (DOT) de[/email] (Michael S.) wrote in message news:<b5aee4d3.0312291335.293c0da4 (AT) posting (DOT) google.com>...
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.
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
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Wed Dec 31, 2003 12:59 am Post subject: Re: Scope-problems |
|
|
Michael S. wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|