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!)
CodePudding user response:
You are trying to default construct two Line
s (width
and length
) in the Rectangle
constructor. Since Line
doesn't have a default constructor you need to use the member initializer list:
Rectangle::Rectangle() : // colon starts the member initializer list
width(Point(0,4), Point(0,0)),
length(Point(0,0), Point(8,0))
{}