实际调用函数时没有匹配的函数调用错误



我目前正在为学校做一个项目,由于某种原因,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

矩形.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);
// }

矩形.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);
}

点.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

我确实遗漏了很多其他不必要的信息,所以如果你需要的话,我的项目就在这里

提前感谢大家,希望大家度过美好的一天(或夜晚!(

您正在尝试在Rectangle构造函数中默认构造两个Line(widthlength(。由于Line没有默认构造函数,因此需要使用成员初始值设定项列表:

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

最新更新