簡體   English   中英

調用隱式刪除的默認構造函數

[英]Call to implicitly-deleted default constructor

當我嘗試編譯我的C ++項目時,我收到錯誤消息調用隱式刪除的'std :: array'的默認構造函數

頭文件cubic_patch.hpp

#include <array>
class Point3D{
public:
    Point3D(float, float, float);
private:
    float x,y,z;
};

class CubicPatch{
public:
    CubicPatch(std::array<Point3D, 16>);
    std::array<CubicPatch*, 2> LeftRightSplit(float, float);
    std::array<Point3D, 16> cp;
    CubicPatch *up, *right, *down, *left;
};

源文件cubic_patch.cpp

#include "cubic_patch.hpp"
Point3D::Point3D(float x, float y, float z){
    x = x;
    y = y;
    z = z;
}

CubicPatch::CubicPatch(std::array<Point3D, 16> CP){// **Call to implicitly-deleted default constructor of 'std::arraw<Point3D, 16>'**
    cp = CP;
}

std::array<CubicPatch*, 2> CubicPatch::LeftRightSplit(float tLeft, float tRight){
    std::array<CubicPatch*, 2> newpatch;
    /* No code for now. */
    return newpatch;
}

有人能告訴我這里有什么問題嗎? 我發現了類似的主題,但並不是真的相同,我不明白給出的解釋。

謝謝。

兩件事情。 類成員在構造函數體之前初始化,默認構造函數是不帶參數的構造函數。

因為您沒有告訴編譯器如何初始化cp,它會嘗試調用std::array<Point3D, 16>的默認構造函數,並且沒有,因為Point3D沒有默認構造函數。

CubicPatch::CubicPatch(std::array<Point3D, 16> CP)
    // cp is attempted to be initialized here!
{
    cp = CP;
}

您可以通過簡單地為構造函數定義提供初始化列表來解決此問題。

CubicPatch::CubicPatch(std::array<Point3D, 16> CP)
    : cp(CP)
{}

此外,您可能希望查看此代碼。

Point3D::Point3D(float x, float y, float z){
    x = x;
    y = y;
    z = z;
}

x = xy = yz = z沒有意義。 您正在為自己分配變量。 this->x = x是一個修復它的選項,但更多c ++樣式選項是使用初始化列表和cp 它們允許您在不使用this->x = x情況下為參數和成員使用相同的名稱

Point3D::Point3D(float x, float y, float z)
    : x(x)
    , y(y)
    , z(z)
{}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM