Gcc Error Constructor Is Private
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error when have private copy ctor with public assignment operator up vote 6 down vote favorite 3 Can one of you explain why the following piece of code does not compile? #include using namespace std; class Foo { public: Foo() { cout << "Foo::Foo()" << endl << endl; } Foo& operator=(const Foo&) { cout << "Foo::operator=(const Foo&)" << endl << endl; } private: Foo(const Foo& b) { *this = b; cout << "Foo::Foo(const Foo&)" << endl << endl; } }; int main() { Foo foo; foo = Foo(); } The error I receive: $ g++ -o copy_ctor_assign copy_ctor_assign.cc && ./copy_ctor_assign copy_ctor_assign.cc: In function 'int main()': copy_ctor_assign.cc:10: error: 'Foo::Foo(const Foo&)' is private copy_ctor_assign.cc:17: error: within this context Note: when I remove the private: keyword the code compiles but the copy ctor is never called. So why does it err when it's private? Not sure if it's important but I'm using: $ g++ --version g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44) Copyright (C) 2006 Free Software Foundation, Inc. c++ copy-constructor assignment-operator share|improve this question edited Jan 20 '13 at 16:43 svick 124k23196308 asked Oct 13 '09 at 15:32 sdumi 634 1 FWIW: The code does compile if you assign a previously created Foo object instead of a temporary object. I.e. foo = bar; instead of foo = Foo();. –sepp2k Oct 13 '09 at 15:47 add a comment| 6 Answers 6 active oldest votes up vote 5 down vote accepted You are initializing a reference from temporary. The standard states: The temporary should be initialized (8.5.3 par 5)"using the rules for a non-reference copy initialization (8.5)". The copy construction is removed for the temporary (permitted by the standard. 12.8 par 5). However, the standard clearly states (12.2 par 1): "Even when the creation of the temporary object is avoided (12.8), all the semantic restrictions must be respected as if the temporary object was created. [Example: even if the copy constructor is not called, all the semantic restrictions, such as accessibility (clau
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up http://stackoverflow.com/questions/1561007/error-when-have-private-copy-ctor-with-public-assignment-operator Private and default constructor in C++11 and gcc up vote 5 down vote favorite Code: struct A { private: A() = default; // Version 1. }; struct B : public A {}; struct C { private: C() {}; // Version 2. }; struct D : public C {}; int main() { B b; // Compiles under g++ 4.7.2 D d; // Compilation error under http://stackoverflow.com/questions/15029586/private-and-default-constructor-in-c11-and-gcc g++ 4.7.2 } And two situations (with gcc 4.7.2): If I compile this code (with the Version 1 of the A's constructor), there aren't problems. If I use the second constructor, gcc says me that D::D() is private. Question: if I use default constructors, why do problems dissapear? If A has private constructors, other classes can never create instances of A, not even its derivated classes, irrespective of the "defaultness" of its constructor's implementation. c++ inheritance c++11 private default-constructor share|improve this question edited Feb 22 '13 at 21:38 Jonathan Wakely 101k9165296 asked Feb 22 '13 at 17:09 Peregring-lk 2,4561440 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote This smells like a GCC bug to me. Neither the whole Clause 11 about member access control nor Section 8.4.2 about defaulted constructors mention anything about overriding the level of accessibility of defaulted constructors. Besides, this code does not compile on Clang 3.2 and Intel ICC 13.0. On the other hand, if you comment line X, the following line will not do what you think: B b(); // This will declare a function that acce
Problem with private copy constructor From: Mathias Froehlich To: gcc-help at https://gcc.gnu.org/ml/gcc-help/2006-06/msg00222.html gcc dot gnu dot org Date: Mon, 26 Jun 2006 11:57:41 +0200 Subject: Problem with private copy constructor Hi, I have a problem with that following code together with gcc-4.1.1. #include class base { public: base() { std::cout << __PRETTY_FUNCTION__ << " " << this gcc error << std::endl; } private: base(const base&); }; class derived : public base { public: derived() { std::cout << __PRETTY_FUNCTION__ << " " << this << std::endl; } }; void fuu(const base& b) { } int main() { fuu(derived()); return 17; } gcc bails out with gcc error constructor the following error message: gcctest2.cpp: In copy constructor 'derived::derived(const derived&)': gcctest2.cpp:8: error: 'base::base(const base&)' is private gcctest2.cpp:11: error: within this context gcctest2.cpp: In function 'int main()': gcctest2.cpp:23: note: synthesized method 'derived::derived(const derived&)' first required here >From my understanding of c++, I would not expect that this copy constructor is called in this case. In fact, gcc-3.3.4 and various other UNIX c++ compilers do not need that copy constructor either. Do I need to dig out my copy of the C++ standard or is it something to file a bugreport? Greetings Mathias -- Dr. Mathias Fröhlich, science + computing ag, Software Solutions Hagellocher Weg 71-75, D-72070 Tuebingen, Germany Phone: +49 7071 9457-268, Fax: +49 7071 9457-511 Follow-Ups: Re: Problem with private copy constructor From: corey taylor Index Nav: [DateIndex] [SubjectIndex] [AuthorIndex] [ThreadIndex] Message Nav: [DatePrev][DateNext] [ThreadPrev][ThreadNext]