“No appropriate default constructor available”--Why is the default constructor even called?

柔情痞子 提交于 2019-11-28 01:50:41
Armen Tsirunyan

Your default constructor is implicitly called here:

ProxyPiece::ProxyPiece(CubeGeometry& c)
{
    cube=c;
}

You want

ProxyPiece::ProxyPiece(CubeGeometry& c)
   :cube(c)
{

}

Otherwise your ctor is equivalent to

ProxyPiece::ProxyPiece(CubeGeometry& c)
    :cube() //default ctor called here!
{
    cube.operator=(c); //a function call on an already initialized object
}

The thing after the colon is called a member initialization list.

Incidentally, I would take the argument as const CubeGeometry& c instead of CubeGeomety& c if I were you.

Member initialization occurs when the constructor begins. If you do not provide an initializer in the constructor's member initialization list, the member will be default constructed. If you want to copy constructor to be used to initialize the member cube, use the member initialization list:

ProxyPiece::ProxyPiece(CubeGeometry& c)
  : cube(c)
{ }

Everything following the colon is the initialization list. This simply says that cube should be initialized with c.

As you had it, the cube member was first default initialized and then c was copy assigned to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!