Question
My program has a circular dependency, and I don't know how to fix it. 1. Issue: the usage of Triangle inside the Circle class without
My program has a circular dependency, and I don't know how to fix it.
1. Issue: the usage of "Triangle" inside the "Circle" class without being actually defined ?? 2. shouldn't perform those forward declarations as attributes of a class, but outside of it. ?? 3. --> Stick with the forward declaration or try to find a way to break the circular dependency including the files properly??? 4. problem we might encounter sticking with the forward declaration is that we should use pointers instead of variables for the classes being forward declared ??
Here is the output I get:
/*********************** triangle.hpp ***********************/ #ifndef __TRIANGLE_H__ #define __TRIANGLE_H__ #include "circle.hpp" class Rectangle; class Circle; class Triangle { double base; double height; friend class Rectangle; friend class Circle; public: Triangle(); Triangle(double b, double h); double getBase(); double getHeight(); void setBase(double b); void setHeight(double h); double area(); bool checkCirc(Circle obj); }; #endif // __TRIANGLE_H__
/*********************** triangle.cpp ***********************/ #include "triangle.hpp" Triangle::Triangle() { base = 1.0; height = 1.0; } Triangle::Triangle(double b, double h) { base = b; height = h; } double Triangle::getBase() { return base; } double Triangle::getHeight() { return height; } void Triangle::setBase(double b) { base = b; } void Triangle::setHeight(double h) { height = h; } double Triangle::area() { return (0.5) * base * height; } bool Triangle::checkCirc(Circle obj) { if (this->area() == obj.area()) return true; return false; } /*********************** rectangle.hpp ***********************/ #ifndef __RECTANGLE_H__ #define __RECTANGLE_H__ #include#include "circle.hpp" class Triangle; class Circle; class Rectangle { double length; double width; friend class Triangle; friend class Circle; public: Rectangle(); Rectangle(double l, double w); double getLength(); double getWidth(); void setLength(double l); void setWidth(double w); double area(); double diagonal(); double perimeter(); bool checkSquare(); bool checkTri(Triangle obj); };
/*********************** rectangle.cpp ***********************/ #endif // __RECTANGLE_H__ #include "rectangle.hpp" Rectangle::Rectangle() { length = 1.0; width = 1.0; } Rectangle::Rectangle(double l, double w) { length = l; width = w; } double Rectangle::getLength() { return length; } double Rectangle::getWidth() { return width; } void Rectangle::setLength(double l) { length = l; } void Rectangle::setWidth(double w) { width = w; } double Rectangle::area() { return length * width; } double Rectangle::diagonal() { return sqrt(length * length + width * width); } double Rectangle::perimeter() { return (2 * (length + width)); } bool Rectangle::checkSquare() { if (length == width) return true; return false; } bool Rectangle::checkTri(Triangle obj) { if (area() == obj.area()) return true; return false; }
/*********************** circle.hpp ***********************/ #ifndef __CIRCLE_H__ #define __CIRCLE_H__ #include "rectangle.hpp" #include "triangle.hpp" class Rectangle; class Triangle; class Circle { double radius; friend class Rectangle; friend class Triangle; public: Circle(); Circle(double r); double getRadius(); void setRadius(double r); double area(); double diameter(); double circumference(); bool checkRect(Rectangle obj); }; #endif // __CIRCLE_H__
/*********************** circle.cpp ***********************/ #include "circle.hpp" Circle::Circle() { radius = 1.0; } Circle::Circle(double r) { radius = r; } double Circle::getRadius() { return radius; } void Circle::setRadius(double r) { radius = r; } double Circle::area() { const double PI = 3.141592; return PI * radius * radius; } double Circle::diameter() { return 2 * radius; } double Circle::circumference() { const double PI = 3.141592; return 2 * PI * radius; } bool Circle::checkRect(Rectangle obj) { if (this->area() == obj.area()) return true; return false; }
/*********************** shapestest.cpp ***********************/ #include#include "circle.hpp" using namespace std; int main() { int choice; while (true) { cout > choice; char ch; if (choice == 1) { cout > ch; double r; if (ch == 'y') { cout > r; Circle c1(r); cout > ch; double b, h; if (ch == 'y') { cout > b; cout > h; Triangle t1(b, h); cout > ch; double l, w; if (ch == 'y') { cout > l; cout > w; Rectangle r1(l, w); cout ----GRASP exec: 84 -8 -o shapestest.exe shapestest.cpp C:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\17273\AppData\Local\Temp shapestest.cpp:31: undefined reference to "Circle::circle(double) shapestest.cpp:32: undefined reference to Circle::diameter() shapestest.cpp:33: undefined reference to 'Circle::area()' shapestest.cpp:34: undefined reference to 'Circle::circumference()' shapestest.cpp:38: undefined reference to "Circle::Circle() shapestest.cpp:39: undefined reference to 'Circle::diameter()' shapestest.cpp:48: undefined reference to 'Circle::arca) shapestest.cpp:41: undefined reference to "Circle::circumference()' shapestest.cpp:55: undefined reference to 'Triangle::Triangle(double, double) shapestest.cpp:56: undefined reference to Triangle::area()' shapestest.cpp:60: undefined reference to 'Triangle::Triangle()' shapestest.cpp:61: undefined reference to "Triangle::area()' shapestest.cpp:75: undefined reference to Rectangle::Rectangle(double, double)" shapestest.cpp:76: undefined reference to 'Rectangle::diagonal) shapestest.cpp:77: undefined reference to Rectangle:: area()' shapestest.cpp:78: undefined reference to Rectangle: :perimeter()' shapestest.cpp:79: undefined reference to Rectangle::checkSquare()' shapestest.cpp:83: undefined reference to Rectangle::Rectangle shapestest.cpp:84: undefined reference to Rectangle::diagonal()' shapestest.cpp:85: undefined reference to 'Rectangle: :area()' shapestest.cpp:86: undefined reference to Rectangle::perimeter() shapestest.cpp:87: undefined reference to Rectangle::checkSquare()' collect2.exe: error: ld returned 1 exit status
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started