我已经查看了与此相关的其他一些问题,但我不明白为什么在我的情况下甚至应该调用默认构造函数。我可以只提供一个默认构造函数,但我想了解它为什么这样做以及它会产生什么影响。
error C2512: 'CubeGeometry' : no appropriate default constructor available
我有一个名为 ProxyPiece 的类,其成员变量为 CubeGeometry。构造函数应该接受 CubeGeometry 并将其分配给成员变量。这是标题:
#pragma once
#include "CubeGeometry.h"
using namespace std;
class ProxyPiece
{
public:
ProxyPiece(CubeGeometry& c);
virtual ~ProxyPiece(void);
private:
CubeGeometry cube;
};
和来源:
#include "StdAfx.h"
#include "ProxyPiece.h"
ProxyPiece::ProxyPiece(CubeGeometry& c)
{
cube=c;
}
ProxyPiece::~ProxyPiece(void)
{
}
立方体几何体的标题如下所示。使用默认构造函数对我来说没有意义。我还需要它吗?:
#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;
};
您的默认构造函数在这里隐式调用:
ProxyPiece::ProxyPiece(CubeGeometry& c)
{
cube=c;
}
You want
ProxyPiece::ProxyPiece(CubeGeometry& c)
:cube(c)
{
}
否则你的 ctor 相当于
ProxyPiece::ProxyPiece(CubeGeometry& c)
:cube() //default ctor called here!
{
cube.operator=(c); //a function call on an already initialized object
}
冒号后面的东西叫做a成员初始化列表 http://www.cprogramming.com/tutorial/initialization-lists-c++.html.
顺便说一句,我会把这个论点视为const CubeGeometry& c
代替CubeGeomety& c
如果我是你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)