实际调用函数时没有匹配的函数调用错误
我目前正在为学校做一个项目,由于某种原因,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 矩形.cpp -o 矩形.o
我确实遗漏了很多其他不必要的信息,所以如果您需要它,我的项目是
提前感谢您,希望大家度过愉快的一天(或夜晚!)
I am currently doing a project for school and for some reason, g++ won't compile my code properly, saying:
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));
}
and the build scripts:
Point: g++ -Wall -c Point.cpp -o Point.o
Line: g++ -Wall -c Line.cpp -o Line.o
Rectangle: g++ -Wall -Wextra -c Rectangle.cpp -o Rectangle.o
I did leave out a lot of other unnecessary information so if you need it, my project is here
Thank you in advance and hope you all have a very nice day (or night!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试在
Rectangle
构造函数中默认构造两个Line
(width
和length
)。由于 Line 没有默认构造函数,因此您需要使用成员初始值设定项列表:You are trying to default construct two
Line
s (width
andlength
) in theRectangle
constructor. SinceLine
doesn't have a default constructor you need to use the member initializer list: