开发者

Why does this code not compile correctly?

I just found my code like this does not compile right? Is there any compiler-provided constructo开发者_运维知识库r here?

class A
  {

   private:   
      A(const A& n);

  };


int main()
{
        A a;
}

The error is test.cpp:18: error: no matching function for call to ‘A::A()’ test.cpp:11: note: candidates are: A::A(const A&)

I am using g++ under Ubuntu 8.04


The compiler will provide for you

  1. the default constructor A() if and only if there are no user-defined constructors, and
  2. the copy constructor A(A const &) unless you provide either of the four possible copy constructors A(A cv &), where cv is any combination of const and volatile.

In your case, you've declared your own copy constructor, which means that the compiler will provide neither of the above.

The line A a; needs an accessible default constructor to compile.


The constructor you declared private in class A is a copy constructor.

Whenever you provide a parameterised constructor for a class C++ won't provide a default constructor ( one taking no arguments ). You have to explicitly define the default class constructor for your class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜