[Bug c++/28365] error: ' error: 'MyClass::MyClass(const MyClass&)' is private error: within this context

fang at csl dot cornell dot edu gcc-bugzilla@gcc.gnu.org
Thu Jul 13 22:06:00 GMT 2006



------- Comment #4 from fang at csl dot cornell dot edu  2006-07-13 22:06 -------
Subject: Re:  error: ' error: 'MyClass::MyClass(const MyClass&)'
 is private   error: within this context

> ------- Comment #3 from kk at microfocus dot com  2006-07-13 21:49 -------
> (In reply to comment #2)
> > If it is in our (Rogue Wave) current code, could you please let us know where
> > so we can look into fixing it? (It's doesn't matter whther gcc does the right
> > thing here or not, our code should be portable either way.) Thanks!
>
> OK, here goes. This is what I'm getting. If you guys do have a fix let me know.
> You have an operator overload for the Expr but it really can't be used in this
> case cause the const constructor is private.

Here's a test case to demonstrate a workaround or solution:
--------------->8 snip 8<----------------
class foo {
private:
        foo(const foo&);
public:
        explicit
        foo(int) { }

        // HERE's the magic
        operator foo& () { return *this; }
};

class streamer {
public:
        streamer&
        operator << (const foo&) {
                return *this;
        }
};

int
main(int, char*[]) {
        streamer s;
        s << foo(0) << foo(1);
        return 0;
}
--------------->8 snip 8<----------------

All I've done is add a reference converter operator to the class with
the private copy-constructor.  Try commenting out the magic line to see
the difference in compilability on 4.0 and higher.

This works with -W -Wall -Werror -ansi -pedantic-errors for
g++ versions 3.3, 4.0, 4.2.  (don't have 3.4, 4.1 handy)

I admit this might not be a pretty solution, but it requires a one-line
addition to get things going.  Comments and suggestions welcome.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28365



More information about the Gcc-bugs mailing list