public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* "xxx is a private member" message
@ 1997-10-29 11:56 Paul Koning
  1997-10-29 13:45 ` Joe Buck
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Koning @ 1997-10-29 11:56 UTC (permalink / raw)
  To: egcs

egcs gives the following message:

foo.cc: In method `void foo::bar()':
foo.cc:12: member `i' is a private member of class `foo'
foo.cc:13: confused by earlier errors, bailing out

while gcc 2.7.2.1 doesn't complain, for the following program:

class foo
{
   int i;
public:
   void bar(void);
};

void foo::bar(void)
{
   struct
   {
      int j[i];
   } k;
   
   k.j[1] = 0;
}

Is that due to a C++ standard change or is something else going on?

	paul

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: "xxx is a private member" message
  1997-10-29 11:56 "xxx is a private member" message Paul Koning
@ 1997-10-29 13:45 ` Joe Buck
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Buck @ 1997-10-29 13:45 UTC (permalink / raw)
  To: Paul Koning; +Cc: egcs

Note: while the example is a bizarre and buggy program, it does expose
a real (minor?) problem in the C++ front end.

> egcs gives the following message:
> 
> foo.cc: In method `void foo::bar()':
> foo.cc:12: member `i' is a private member of class `foo'
> foo.cc:13: confused by earlier errors, bailing out
> 
> while gcc 2.7.2.1 doesn't complain, for the following program:

What a bizarre program.  What does it do?  Variable-length arrays are a
GNU extension, but the reference to i: which i do you expect it to get?
The i belonging to "this" when foo is called?  I doubt if 2.7.2.1's
behavior on this construct was more than an accident.

> class foo
> {
>    int i;
> public:
>    void bar(void);
> };
> 
> void foo::bar(void)
> {
>    struct
>    {
>       int j[i];
>    } k;
>    
>    k.j[1] = 0;
> }

> Is that due to a C++ standard change or is something else going on?

C++ never said you could do that.  Ignoring the fact that it is a GNU
extension, a local class cannot refer to non-static members of another
class in this way.

The error message you're getting isn't really right.  Let's consider
another program that uses the same idea, only I'll use a vector and
change foo::bar to

#include <vector>

void foo::bar(void)
{
    struct zed {
	vector<int> j;
	zed() : j(i);
    } k;

    k.j[1] = 0;
}

Now the struct has a variable-sized vector, not a variable-sized array.
This is still not a legal program, though the error message isn't quite
right.  egcs says

b.cc: In method `foo::bar()::zed::zed()':
b.cc:15: `struct foo::bar()::zed' has no member named `i'

Well, yes, but that isn't the problem.  (at least it didn't dump core
like I was afraid it would :-)

HP's aCC says

Error 178: "b.cc", line 15 # Nonstatic member "int foo::i" is referenced
in a nested class, local class or static member initializer.
           zed() : j(i) {}

This accurately diagnoses the problem: you can't do this.  You can't
use fields belonging to only one object in this context.  I suggest that
we add a similar diagnostic to egcs/g++.

Now, it isn't to hard to get a legal program that uses i for the size
of the vector:

void foo::bar(void)
{
    struct zed {
	vector<int> j;
	zed(int sz) : j(sz);
    } k(i);

    k.j[1] = 0;
}

This one compiles.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-10-29 13:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-29 11:56 "xxx is a private member" message Paul Koning
1997-10-29 13:45 ` Joe Buck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).