Implementation file problem.

Could someone explain why my class 'Rectangle' isn't recognized in my implementation file?

Rectangle.hpp
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
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef RECTANGLE_HPP_INCLUDED
#define RECTANGLE_HPP_INCLUDED
#include "Rectangle.cpp"
#include <math.h>
#include <iostream>

struct Point
{
    double x;
    double y;
};

class Rectangle
{
    private:
        double x1, y1, x2, y2, x3, y3, x4, y4;
        double rectLength;
        double rectWidth;
    public:
        Rectangle(Point &cord1, Point &cord2, Point &cord3, Point &cord4)
        {
            x1 = cord1.x;
            y1 = cord1.y;
            x2 = cord2.x;
            y2 = cord2.y;
            x3 = cord3.x;
            y3 = cord3.y;
            x4 = cord4.x;
            y4 = cord4.y;
        }
        void length();
        void width();
        void perimeter();

};

#endif // RECTANGLE_HPP_INCLUDED


Rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <math.h>
#include <iostream>
#include "Rectangle.hpp"

void Rectangle::length()
{
    rectLength = fabs(x1 - x2);
     std::cout << rectLength;
}

void Rectangle::width()
{
    rectWidth = fabs(y1 - y2);
    std::cout << rectWidth;
}
Last edited on
are you sure that your hpp file is named "Rectangle.hpp"?

1
2
3
 
#ifndef RECTANGLE_HPP_INCLUDED
#define RECTANGLE_HPP_INCLUDED 


I also believe it is not necessary to call

#include "Rectangle.cpp"

in hpp files
Last edited on
Yes, both my .cpp and .hpp files are named 'Rectangle'.
Never #include .cpp files. In general, don't #include a file that #includes the file you're #including from.
I can see the main?
Here is my code from main.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// A Sophisticated Rectangle Class
#include <iostream>
#include <stdexcept>
#include "Rectangle.hpp" // include definition of class Rectangle
/*

Create a sophisticated Rectangle class. This class stores only the Cartesian coordinates of the four corners of the
rectangle. The constructor calls a set function that accepts four sets of coordinates and verifies that
each of these is in the first quadrant with no single x- or y-coordinate larger than 20.0. The set function
also verifies that the supplied coordinates do, in fact, specify a rectangle.

Provide member functions that calculate the length, width, perimeter and area. The length is the larger of the two
dimensions.

Include a predicate function square that determines whether the rectangle is a square.
*/

int main() {
   Point w{1.0, 1.0};
   Point x{5.0, 1.0};
   Point y{5.0, 3.0};
   Point z{1.0, 3.0};
   Point j{0.0, 0.0};
   Point k{1.0, 0.0};
   Point m{1.0, 1.0};
   Point n{0.0, 1.0};

   Rectangle r1{z, y, x, w};
   std::cout << "Rectangle 1:\n";
   std::cout << "length = " << r1.length();
   std::cout << "\nwidth = " << r1.width();
   r1.perimeter();
   r1.area();
   std::cout << "\nThe rectangle "
      << (r1.square() ? "is" : "is not")
      << " a square.\n";

   Rectangle r2{j, k, m, n};
   std::cout << "\nRectangle 2:\n";
   std::cout << "length = " << r2.length();
   std::cout << "\nwidth = " << r2.width();
   r2.perimeter();
   r2.area();
   std::cout << "\nThe rectangle "
      << (r2.square() ? "is" : "is not")
      << " a square.\n";

}
I took out the #include "Rectangle.cpp" and that solved my problem. Thanks for the solutions.
Topic archived. No new replies allowed.