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

后端 未结 2 426
广开言路
广开言路 2020-12-01 23:30

I\'ve looked at a few other questions about this, but I don\'t see why a default constructor should even be called in my case. I could just provide a default constructor, bu

2条回答
  •  感动是毒
    2020-12-02 00:28

    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.

提交回复
热议问题