简体   繁体   English

class 不存在默认构造函数

[英]No default constructor exists for the class

I know this question has already been ask, but I couldn't figure it out.我知道这个问题已经被问过了,但我无法弄清楚。 I have two classes Point and Line, and 2 Points are members of Line.我有两个类 Point 和 Line,2 Points 是 Line 的成员。 However in the Line constructor I get "no default constructor exists for the class" error.但是,在 Line 构造函数中,我得到“类没有默认构造函数”错误。 How can I fix this problem?我该如何解决这个问题?

#include <cstdlib>
#include <cmath>
#include "PointClass.h"
using namespace std;
class Line {
public:

    Line(const Point& p1, const Point& p2) {
        this->point1 = p1;
        this->point2 = p2;
    }
    Point point1;
    Point point2;

    static double Distance(Point p1, Point p2, Point p3) {
        double distance = (abs((p1.y - p2.y) * p3.x - (p2.x - p1.x) * p3.y + p2.x * p1.y - p2.x * p1.x) / (sqrt(pow((p2.y - p1.y), 2.0) + pow((p2.x - p1.x), 2.0))));
            return distance;
    }

};
class Point {
public:
    Point(double a, double b) {
        this->setCoord(a, b);
    }
    double x;
    double y;
    void setCoord(double a, double b)
    {
        this->x = a;
        this->y = b;
    }


};

The reason for you error, is that this code calls the Point default constructor (which doesn't exist)您出错的原因是此代码调用 Point 默认构造函数(不存在)

Line(const Point& p1, const Point& p2) {
    this->point1 = p1;
    this->point2 = p2;
}

instead you should write it like this相反,你应该这样写

Line(const Point& p1, const Point& p2) : point1(p1), point2(p2) {
}

Your version calls the Point default constructor and then assigns the point values.您的版本调用 Point 默认构造函数,然后分配点值。 My version initialises the points by calling the Point copy constructor我的版本通过调用 Point 复制构造函数来初始化

The error message says "no default constructor", so you should add ones.错误消息说“没有默认构造函数”,所以你应该添加一个。

class Line {
public:
    Line() {} // add this

    Line(const Point& p1, const Point& p2) {
class Point {
public:
    Point() {} // add this
    Point(double a, double b) {

or ones with initialization (safer):或具有初始化的(更安全):

class Line {
public:
    Line() : point1(), point2() {} // add this

    Line(const Point& p1, const Point& p2) {
class Point {
public:
    Point() : x(0), y(0) {} // add this
    Point(double a, double b) {

or constructors with default arguments:或具有默认 arguments 的构造函数:

The core reason you are getting that error.您收到该错误的核心原因。 Is because Line constructor doesn't know how to initialize point1 and point2 prior to your assignment statements in the constructor.是因为Line构造函数不知道如何在构造函数中的赋值语句之前初始化point1point2 That's what constructor initialization lists are for.这就是构造函数初始化列表的用途。 It's usually better to do initial member initialization in the constructor initialization list instead of in the constructor body.通常最好在构造函数初始化列表中而不是在构造函数主体中进行初始成员初始化。 Without a constructor initialization list, point1 and point2 get constructed with the default constructor (error because it's missing) and then immediate updated with additional code in constructor body.如果没有构造函数初始化列表, point1point2将使用默认构造函数构造(错误,因为它丢失),然后立即使用构造函数主体中的附加代码进行更新。 You can avoid the need for a default constructor on Point by having Line constructor defined as follows:您可以通过如下定义Line构造函数来避免在Point上使用默认构造函数:

Line(const Point& p1, const Point& p2) : point1(p1), point2(p2)
{}

That will resolve your compiler error.这将解决您的编译器错误。 Further, it's still not a bad idea to have a default constructor for Point.此外,为 Point 使用默认构造函数仍然不是一个坏主意。 It's the type of class where it's often useful to have such a constructor.它是 class 的类型,拥有这样的构造函数通常很有用。 And some point later, you might need to have a collection of Point instances and the compiler will complain again without it.稍后,您可能需要收集Point实例,如果没有它,编译器会再次抱怨。 MakeCAT's answer is correct in that regards. MakeCAT 在这方面的回答是正确的。

Aside : Your Distance function is passing Point parameters by value.旁白:您的Distance function正在按值传递Point参数。 This means the compiler needs to construct 3 new instances of Point each time Distance is invoked.这意味着每次调用Distance时编译器都需要构造 3 个新的Point实例。 Change your function signature for Distance as follows.如下更改Distance的 function 签名。 If this doesn't make the compiler happy, it will at the very least, generate more efficient code.如果这不能让编译器满意,它至少会生成更高效的代码。

static double Distance(const Point& p1, const& Point p2, const& Point p3) {
        double distance = (abs((p1.y - p2.y) * p3.x - (p2.x - p1.x) * p3.y + p2.x * p1.y - p2.x * p1.x) / (sqrt(pow((p2.y - p1.y), 2.0) + pow((p2.x - p1.x), 2.0))));
            return distance;
    }

The problem is that you don't have a default constructor for that class and therefore when you create the points you don't know what values their variables will take.问题是您没有该 class 的默认构造函数,因此当您创建点时,您不知道它们的变量将采用什么值。 The easiest and fastest way of solutions is to give default values to the constructor when it does not receive parameters最简单最快的解决方法是在构造函数不接收参数时给它默认值

class Point {
public:
//only modification here
Point(double a = 0, double b = 0) {
    this->setCoord(a, b);
}
double x;
double y;
void setCoord(double a, double b)
{
    this->x = a;
    this->y = b;
}

}; };

The default constructor is the constructor without parameters.默认构造函数是不带参数的构造函数。 If you have a user provided constructor taking parameters (like your Line constructor taking two Point s) then you don't automatically get the default constructor, you need to add it yourself.如果您有一个用户提供的带有参数的构造函数(例如带有两个PointLine构造函数),那么您不会自动获取默认构造函数,您需要自己添加它。

I suggest changing your classes我建议改变你的课程

  1. to use in-class initializers使用类内初始化器
  2. to use member initialization instead of setting in the constructor body使用成员初始化而不是在构造函数主体中设置
  3. defaulting the default constructor (which requires step 1)默认默认构造函数(需要第 1 步)

You can compare the IsoCppCoreguidelines for a more detailed explanation on these changes.您可以比较IsoCppCore 指南以获取有关这些更改的更详细说明。


class Point {
public:
    Point() = default;
    Point(double a, double b) : x{a}, y{b} {}
    double x{};
    double y{};
};

class Line {
public:
    Line() = default;
    Line(const Point& p1, const Point& p2) : point1{p1}, point2{p2} {}
    Point point1{};
    Point point2{};
};

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

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