g++ Compilation problem with implicitly declared copy constructor

Solution Verified - Updated -

Issue

The following C++ example will not compile on any gcc version:

class noncopyable {
   public:
      noncopyable() {}
      ~noncopyable() {}
   private:
      noncopyable( const noncopyable& );
      const noncopyable& operator=( const noncopyable& );
};

class Foo : public noncopyable {
   public:
      Foo () {}
};

static Foo func1() { 
      return Foo();
}

int main() {
      func1();
      return 0;
}

Compiling the example above with g++ will give the following error:

test2.cpp: In copy constructor ‘Foo::Foo(const Foo&)’:
test2.cpp:9: error: ‘noncopyable::noncopyable(const noncopyable&)’ is private
test2.cpp:15: error: within this context
test2.cpp: In function ‘Foo func1()’:
test2.cpp:24: note: synthesized method ‘Foo::Foo(const Foo&)’ first required here 

Environment

  • RHEL
  • gcc

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content