473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 22 '05 #1
6 1306
"Michael S." <ey***********@web.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
Jul 22 '05 #2
Hi,

ey***********@web.de (Michael S.) wrote in message news:<b5**************************@posting.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.
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
Jul 22 '05 #3
Wagner Bruna wrote:
Hi,

ey***********@web.de (Michael S.) wrote in message news:<b5**************************@posting.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/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

Jul 22 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<Gl3Ib.696801$Fm2.600116@attbi_s04>...
"Michael S." <ey***********@web.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
Jul 22 '05 #5
Jeff Schwab <je******@comcast.net> wrote in message news:<U6********************@comcast.com>...
Wagner Bruna wrote:
Hi,

ey***********@web.de (Michael S.) wrote in message news:<b5**************************@posting.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/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

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
Jul 22 '05 #6
Michael S. wrote:
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.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers...
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
3
by: marco_segurini | last post by:
Hi, I am using VS 2005. If I compile the following code only line 6 returns me an error while line 9 returns a warning. If I comment the line 6 and debug the program the assignments of lines...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
1
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid,...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.