Unable to compile, assistance needed

Pages: 12
Edit: looks like you solved it while I was typing. Yes, that's right - glad we could help ;)

Original post:
No, but you're close. pt1 is an actual Point2D, not Line2D. You would use the getX and getY member functions. Since you're killing me slowly, I'll just write the line here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
length
=
sqrt
(
    pow
    (
        (
            pt1.getX()
            -
            pt2.getX()
        )
        ,
        2
    )
    +
    pow
    (
        (
            pt1.getY()
            -
            pt2.getY()
        )
        ,
        2
    )
);
Last edited on
Well, this may be a little bit too much. But I would still hope you experts could help a a little bit more :)

Now I've another class here which is the Line3D class. I'm not sure if I could do the #include just like this

Line3D.h
===========

#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include "Point2D.h"
#include "Point3D.h"
#include "Line2D.h"



using namespace std;

class Line3D : public Line2D
{
private:
Point3D pt1;
Point3D pt2;

protected:
void setLength();

public:
Line3D(Point3D, Point3D);
Point3D getPt1();
Point3D getPt2();

void setPt1(Point3D);
void setPt2(Point3D);

};

===========
Line3D.cpp
===========

#include "Line3D.h"

using namespace std;

Line3D::Line3D(Point3D point1, Point3D point2)
{
pt1 = point1;
pt2 = point2;
}

Point3D Line3D::getPt1()
{
return pt1;
}

Point3D Line3D::getPt2()
{
return pt2;
}

void Line3D::setPt1(Point3D SetPt1)
{
pt1 = SetPt1;
}

void Line3D::setPt2(Point3D SetPt2)
{
pt2 = SetPt2;
}

void Line3D::setLength()
{
length = sqrt (pow((pt1.getX() - pt2.getX()),2) + pow((pt1.getY() - pt2.getY()),2) + pow((pt1.getZ() - pt2.getZ()),2));

}



===========
Errors
===========

In file included from Point3D.h:5,
from Line3D.h:6:
Point2D.h:9: error: redefinition of ‘class Point2D’
Point2D.h:10: error: previous definition of ‘class Point2D’
In file included from Line2D.h:5,
from Line3D.h:7:
Point2D.h:9: error: redefinition of ‘class Point2D’
Point2D.h:10: error: previous definition of ‘class Point2D’
Line3D.cpp: In constructor ‘Line3D::Line3D(Point3D, Point3D)’:
Line3D.cpp:5: error: no matching function for call to ‘Line2D::Line2D()’
Line2D.h:20: note: candidates are: Line2D::Line2D(Point2D, Point2D)
Line2D.h:10: note: Line2D::Line2D(const Line2D&)
Line3D.cpp:5: error: no matching function for call to ‘Point3D::Point3D()’
Point3D.h:16: note: candidates are: Point3D::Point3D(int, int, int)
Point3D.h:10: note: Point3D::Point3D(const Point3D&)
Line3D.cpp:5: error: no matching function for call to ‘Point3D::Point3D()’
Point3D.h:16: note: candidates are: Point3D::Point3D(int, int, int)
Point3D.h:10: note: Point3D::Point3D(const Point3D&)
Oh, hi Peter87! I remember you :)

Just FYI I've referred back to your 1st post and now reduce the errors to as follows. :)


In file included from Point3D.h:5,
from Line3D.h:6:
Point2D.h:9: error: redefinition of ‘class Point2D’
Point2D.h:10: error: previous definition of ‘class Point2D’
In file included from Line2D.h:5,
from Line3D.h:7:
Point2D.h:9: error: redefinition of ‘class Point2D’
Point2D.h:10: error: previous definition of ‘class Point2D’
Line3D.cpp: In constructor ‘Line3D::Line3D(Point3D, Point3D)’:
Line3D.cpp:5: error: no matching function for call to ‘Line2D::Line2D()’
Line2D.h:20: note: candidates are: Line2D::Line2D(Point2D, Point2D)
Line2D.h:10: note: Line2D::Line2D(const Line2D&)
That's because in the constructor instead of

Line3D::Line3D(Point3D point1, Point3D point2)
{
pt1 = point1;
pt2 = point2;
}

I added the... : pt1(point1),pt2(point2) behind

Line3D::Line3D(Point3D point1, Point3D point2) : pt1(point1),pt2(point2)
{
pt1 = point1;
pt2 = point2;
}
:(

I'm now left with these error, can anyone please advise or assist? please....


In file included from Point3D.h:5,
from Line3D.h:7:
Point2D.h:9: error: redefinition of ‘class Point2D’
Point2D.h:10: error: previous definition of ‘class Point2D’
Line3D.cpp: In constructor ‘Line3D::Line3D(Point3D, Point3D)’:
Line3D.cpp:5: error: no matching function for call to ‘Line2D::Line2D()’
Line2D.h:20: note: candidates are: Line2D::Line2D(Point2D, Point2D)
Line2D.h:10: note: Line2D::Line2D(const Line2D&)
as Peter87 stated: use include guards in all of your headers!

Line3D.cpp:5: error: no matching function for call to ‘Line2D::Line2D()’
You need a constructor with no parameter in your Line2D class
My constructor is mandatory to have parameters...
Your Line3D class inherits from Line2D. This means that when you create an object of Line 3D, it implicitly calls a constructor on Line2D.

Your constructor for Line3D doesn't specify how to construct Line2D, so the compiler is assuming you want it to call a default constructor on Line2D, i.e. one that takes no parameters. You haven't defined one, so it is failing.

Your constructor for Line3D needs to specify how to use the Line2D constructor you have defined.

Your code should look something like:

1
2
3
4
5
6
7
Line3D::Line3D(Point3D point1, Point3D point2)
: Line2D(/*Put the arguments to the desired Line2D constructor here*/),
  pt1(point1),
  pt2(point2)
{
  /* Contents of your constructor */
}


And, please, when posting code to this forum, could you use the code tags? After all, you want us to be able to read your code easily, right? To create the tags, use the "<>" button to the right of the edit window, and then put your code between the tags.
Last edited on
Thank you Mikey Boy, that explains. I was still wondering the codes you guys posted looks neater. That's how u do it.

So I've come up with something, is this how I should code my constructor in Line3D clss

1
2
3
4
5
Line3D::Line3D(Point3D point1, Point3D point2) : Line2D(point1, point2) pt1(point1),pt2(point2)
{
	pt1 = point1;
	pt2 = point2;
}
Yes, it reduce more errors. Now I'm just left with 1 problem if I'm not wrong. Which is to make use to the #include guard. I'm not really how how to use the include guard. But here are my remaining errors

In file included from Point3D.h:5,
                 from Line3D.h:7:
Point2D.h:11: error: redefinition of ‘class Point2D’
Point2D.h:12: error: previous definition of ‘class Point2D’

Include guards stop you accidentally including the body of a header file more than once in a single source code file. In your case, one of your source files is including "Point2D.h" twice.

You use preprocessor directives to prevent this happening. The Wiki link Peter posted contains a clear example of how to do this.

You should do it as a matter of course in pretty much every single header file you ever write.
Last edited on
Topic archived. No new replies allowed.
Pages: 12