简体   繁体   English

“没有合适的默认构造函数可用”-为什么还要调用默认构造函数?

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

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. 我有一个名为ProxyPiece的类,其成员变量为CubeGeometry。构造函数应该采用CubeGeometry并将其分配给成员变量。 Here is the header: 这是标题:

#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;
};

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& c instead of CubeGeomety& c if I were you. 顺便说一句,如果我是您,我将采用const CubeGeometry& c而不是CubeGeomety& c作为参数。

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 cube should be initialized with c . 这只是说cube应使用c初始化。

As you had it, the cube member was first default initialized and then c was copy assigned to it. 如您所愿,首先将cube成员默认初始化,然后将c 复制分配给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM