C++ “没有合适的默认构造函数可用”--为什么甚至调用默认构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15710125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:40:59  来源:igfitidea点击:

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

c++constructormemberdefault-constructor

提问by AAB

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, but I want to understand why it is doing this and what it affects.

我已经查看了其他一些关于此的问题,但我不明白为什么在我的情况下甚至应该调用默认构造函数。我可以只提供一个默认构造函数,但我想了解它为什么这样做以及它会影响什么。

error C2512: 'CubeGeometry' : no appropriate default constructor available  

I have a class called ProxyPiece with a member variable of CubeGeometry.The constructor is supposed to take in a CubeGeometry and assign it to the member variable. Here is the header:

我有一个名为 ProxyPiece 的类,其成员变量为 CubeGeometry。构造函数应该接收 CubeGeometry 并将其分配给成员变量。这是标题:

#pragma once
#include "CubeGeometry.h"

using namespace std;
class ProxyPiece
{
public:
    ProxyPiece(CubeGeometry& c);
    virtual ~ProxyPiece(void);
private:
    CubeGeometry cube;
};

and the source:

和来源:

#include "StdAfx.h"
#include "ProxyPiece.h"

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


ProxyPiece::~ProxyPiece(void)
{
}

the header for cube geometry looks like this. It doesn't make sense to me to use a default constructor. Do I need it anyways?:

立方体几何的标题看起来像这样。使用默认构造函数对我来说没有意义。我还需要它吗?:

#pragma once
#include "Vector.h"
#include "Segment.h"
#include <vector>

using namespace std;

class CubeGeometry
{
public:
    CubeGeometry(Vector3 c, float l);

    virtual ~CubeGeometry(void);

    Segment* getSegments(){
        return segments;
    }

    Vector3* getCorners(){
        return corners;
    }

    float getLength(){
        return length;
    }

    void draw();

    Vector3 convertModelToTextureCoord (Vector3 modCoord) const;

    void setupCornersAndSegments();

private:
    //8 corners
    Vector3 corners[8];

    //and some segments
    Segment segments[12];

    Vector3 center;
    float length;
    float halfLength;
};

回答by 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

否则你的ctor相当于

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& cinstead of CubeGeomety& cif I were you.

顺便说一句,如果我是你,我会认为这个论点const CubeGeometry& c而不是CubeGeomety& c你。

回答by Joseph Mansfield

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:

成员初始化发生在构造函数开始时。如果在构造函数的成员初始化列表中未提供初始化程序,则该成员将被默认构造。如果要复制构造函数以用于初始化成员cube,请使用成员初始化列表:

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

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

冒号后面的所有内容都是初始化列表。这只是说cube应该用c.

As you had it, the cubemember was first default initialized and then cwas copy assignedto it.

正如您所拥有的,该cube成员首先被默认初始化,然后c复制分配给它。