Undefined reference to functions

Could someone explain why every time I call a function in main, it says it is a undefined reference to it?

main.cpp
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
49
// 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";

}


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 <math.h>
struct Point
{
    double x;
    double y;
};

class Rectangle
{
    private:
        double x1, y1, x2, y2, x3, y3, x4, y4;
        double rectLength;
        double rectWidth;
        double rectArea;
        double rectPerimeter;
    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;
        }
        double length();
        double width();
        void area();
        void perimeter();
        bool square();

};

#endif // RECTANGLE_HPP_INCLUDED 


Rectangle.cpp
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
class Rectangle;
double Rectangle::length()
{
    rectLength = fabs(x1 - x2);
     return rectLength;
}

double Rectangle::width()
{
    rectWidth = fabs(y1 - y2);
    return rectWidth;
}

void Rectangle::area()
{
    rectArea = rectLength * rectWidth;
}

void Rectangle::perimeter()
{
    rectPerimiter = (2 * rectLength) + (2 * rectWidth);
}

bool Rectangle::square()
{
    if(rectLength == rectWidth)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
Make sure that Rectangle.cpp is part of the project (or is specified separately on the command line along with the resulting object file when linking.)
In Rectangle.hpp, #include <cmath> , which is the C++ header, and not #include <math.h> , which is the C header.

In Rectangle.cpp, you need to #include "Rectangle.hpp" and remove the class declaration of Rectangle.
Thank you for all the suggestions. The main problem was that they were not all part of the same project. By adding them I was able to resolve my issue.
Topic archived. No new replies allowed.