实际调用函数时No matching function call错误

No matching function call error when the functions are actually being called

我目前正在为学校做一个项目,由于某种原因,g++ 无法正确编译我的代码,说:

Rectangle.cpp: In constructor ‘Rectangle::Rectangle()’:
Rectangle.cpp:8:22: error: no matching function for call to ‘Line::Line()’
 Rectangle::Rectangle(){
                      ^
In file included from Rectangle.h:1:0,
                 from Rectangle.cpp:2:
Line.h:7:5: note: candidate: Line::Line(Point, Point)
     Line(Point s, Point e);
     ^~~~
Line.h:7:5: note:   candidate expects 2 arguments, 0 provided
Line.h:3:7: note: candidate: constexpr Line::Line(const Line&)
 class Line{
       ^~~~
Line.h:3:7: note:   candidate expects 1 argument, 0 provided
Line.h:3:7: note: candidate: constexpr Line::Line(Line&&)
Line.h:3:7: note:   candidate expects 1 argument, 0 provided
Rectangle.cpp:8:22: error: no matching function for call to ‘Line::Line()’
 Rectangle::Rectangle(){
                      ^
In file included from Rectangle.h:1:0,
                 from Rectangle.cpp:2:
Line.h:7:5: note: candidate: Line::Line(Point, Point)
     Line(Point s, Point e);
     ^~~~
Line.h:7:5: note:   candidate expects 2 arguments, 0 provided
Line.h:3:7: note: candidate: constexpr Line::Line(const Line&)
 class Line{
       ^~~~
Line.h:3:7: note:   candidate expects 1 argument, 0 provided
Line.h:3:7: note: candidate: constexpr Line::Line(Line&&)
Line.h:3:7: note:   candidate expects 1 argument, 0 provided
Rectangle.mak:11: recipe for target 'Rectangle.o' failed
make: *** [Rectangle.o] Error 1

Rectangle.cpp

#include <iostream>
#include "Rectangle.h"
using namespace std;
// Rectangle::Rectangle(Line w, Line l){
//   width=w;
//   length=l;
// };
Rectangle::Rectangle(){
  Line w(Point(0,4),Point(0,0));
  width=w;
  Line l(Point(0,0),Point(8,0));
  length=l;
}
// Rectangle::Rectangle(Point ws, Point we, Point ls, Point le) {
//   width=Line(ws,we);
//   length=Line(ls,le);
// }

Rectangle.h

#include "Line.h"
class Rectangle{
  public:
    void print();
    double calcArea();
    Rectangle();
    // Rectangle(Line w, Line l);
    // Rectangle(Point ws, Point we, Point ls, Point le);
  private:
    Line width;
    Line length;
};

Line.h

#include "Point.h"
// struct Line;
class Line{
  public:
    void print();
    double lineLength();
    Line(Point s, Point e);
  private:
    Point start;
    Point end;
};

Line.cpp

#include <iostream>
#include "Line.h"
using namespace std;
Line::Line(Point s, Point e){
  start=s;
  end=e;
}
void Line::print() {
  cout << " Start";
  start.print();
  cout << " End";
  end.print();
  cout << endl;
}
double Line::lineLength(){
  return start.calcDistance(end);
}

Point.h

class Point{
    public:
        Point();
        Point(double xVal,double yVal);
        // double getX();
        // double getY();
        // void setX(double xVal);
        // void setY(double yVal);
        void print();
        double calcDistance(Point a);
    private:
        double x;
        double y;
};

Point.cpp

#include "Point.h"
#include <math.h>
#include <iostream>
using namespace std;



Point::Point() {
    x = 0.0;
    y = 0.0;
}
Point::Point(double xVal, double yVal){
    x=xVal;
    y=yVal;
}
// double Point::getX() {
//     return x;
// }
// double Point::getY() {
//     return y;
// }
// void Point::setX(double xVal) {
//     x = xVal;
// }
// void Point::setY(double yVal) {
//     y = yVal;
// }
void Point::print(){
  cout<<"x:\t"<<x<<"\ny:\t"<<y<<endl;
}
double Point::calcDistance(Point a){
  return sqrt(pow(x-a.x,2)+pow(y-a.y,2));
}

和构建脚本:
g++ -Wall -c Point.cpp -o Point.o
g++ -Wall -c Line.cpp -o Line.o
矩形: g++ -Wall -Wextra -c Rectangle.cpp -o Rectangle.o

我确实遗漏了很多其他不必要的信息所以如果你需要它,我的项目是 here

在此先感谢您,希望大家度过愉快的一天(或一晚!)

您正在尝试在 Rectangle 构造函数中默认构造两个 Linewidthlength)。由于 Line 没有默认构造函数,您需要使用成员初始化列表:

Rectangle::Rectangle() : // colon starts the member initializer list
    width(Point(0,4), Point(0,0)),
    length(Point(0,0), Point(8,0))
{}