I'm new to OOP, and am trying to figure my way through inheritance. I have read multiple other posts on here about people who ran into the same issues. From what I could tell many of their issues came from the way their classes were inherited. I thought I had all my ducks in a row, but the compiler keeps throwing me errors. The examples that I have found and read through in the book I'm using (Absolute C++, 6th edition), hasn't covered errors like this yet. I included all the .cpp, and .h files. Any help pointing me in the right direction would be gratefully appreciated.
point.h
#ifndef The_points_H
#define The_points_H
#include <iostream>
class Point{
public:
Point(int Xval, int Yval);
Point();
int getX() const;
int getY() const;
void setX(int Xval);
void setY(int Yval);
private:
int x;
int y;
};
#endif
point.cpp
#include "point.h"
Point::Point(int Xval, int Yval): x(Xval), y(Yval) {}
Point::Point(): x(0), y(0) {}
int Point::getX() const {
return x;
}
int Point::getY() const {
return y;
}
void Point::setX(int Xval) {
x = Xval;
}
void Point::setY(int Yval) {
y = Yval;
}
rectangle.h
#ifndef The_rectangle_H
#define The_rectangle_H
#include "point.h"
#include <iostream>
class Rectangle: public Point {
public:
Rectangle(Point(), int h, int w);
Rectangle();
Point getTL() const;
int getHeight() const;
int getWidth() const;
void setHeight(int h);
void setWidth(int w);
Rectangle makeRectangle(Rectangle rect1);
//friend Rectangle operator %(const Rectangle& rect1, const Rectangle& rect2);
private:
Point tL;
int height;
int width;
};
#endif
rectangle.cpp
#include <iostream>
#include "point.h"
#include "rectangle.h"
Rectangle::Rectangle(Point(), int h, int w) : tL(Point()), height(h), width(w) {}
Rectangle::Rectangle() : tL(Point()), height(0), width(0){}
Point Rectangle::getTL() const{
return tL;
}
int Rectangle::getHeight() const{
return height;
}
int Rectangle::getWidth() const{
return width;
}
void Rectangle::setHeight(int h) {
height = h;
}
void Rectangle::setWidth(int w) {
width = w;
}
Rectangle Rectangle::makeRectangle(Rectangle rect1) {
int xPoint1;
int yPoint1;
int heIgth1;
int wIdth1;
std::cout << "Enter x1 coordinate: " << std::endl;
std::cin >> xPoint1;
std::cin.clear();
std::cout << "Enter y1 coordinate: " << std::endl;
std::cin >> yPoint1;
std::cin.clear();
std::cout << "Enter the height1: " << std::endl;
std::cin >> heIgth1;
std::cin.clear();
std::cout << "Enter the width1: " << std::endl;
std::cin >> wIdth1;
std::cin.clear();
Point p1(xPoint1, yPoint1);
Rectangle rect(p1, heIgth1, wIdth1);
return rect;
}
rect_funcs.h
#ifndef Rect_funcs_H
#define Rect_funcs_H
#include <iostream>
#include "point.h"
#include "rectangle.h"
//Rectangle makeRectangle();
void show_Coordinates(Rectangle rect1);
bool operator %(const Rectangle& rect1, const Rectangle& rect2);
#endif
rect_funcs.cpp
#include <iostream>
#include "point.h"
#include "rectangle.h"
#include "rect_funcs.h"
//Function to create anchor point, and get heigth/width values
/*
Rectangle makeRectangle() {
int xPoint1;
int yPoint1;
int heIgth1;
int wIdth1;
std::cout << "Enter x1 coordinate: " << std::endl;
std::cin >> xPoint1;
std::cin.clear();
std::cout << "Enter y1 coordinate: " << std::endl;
std::cin >> yPoint1;
std::cin.clear();
std::cout << "Enter the height1: " << std::endl;
std::cin >> heIgth1;
std::cin.clear();
std::cout << "Enter the width1: " << std::endl;
std::cin >> wIdth1;
std::cin.clear();
Point p1(xPoint1, yPoint1);
Rectangle rect(p1, heIgth1, wIdth1);
return rect;
}
*/
//Overloading of % operator to check if rectangles overlap or not
bool operator %(const Rectangle& rect1, const Rectangle& rect2){
Point top_Left1 = rect1.getTL();
Point bottom_Right1((rect1.getX() + rect1.getWidth()), (rect1.getY() - rect1.getHeight()));
Point top_Left2 = rect2.getTL();
Point bottom_Right2((rect2.getX() + rect2.getWidth()), (rect2.getY() - rect2.getHeight()));
if(top_Left1.getX() == bottom_Right1.getX() || top_Left1.getY() == bottom_Right1.getY() || top_Left2.getX() == bottom_Right2.getX() ||
top_Left2.getY() == bottom_Right2.getY())
return false;
if(top_Left1.getX() >= bottom_Right2.getX() || top_Left2.getX() >= bottom_Right1.getX())
return false;
if(top_Left1.getY() >= bottom_Right2.getY() || top_Left2.getY() >= bottom_Right1.getY())
return false;
return true;
}
void show_Coordinates(Rectangle rect1){
int x = rect1.getX();
int y = rect1.getY();
int heigth = rect1.getHeight();
int width = rect1.getWidth();
std::cout << "(" << x << "," << y << "), " << "(" << (x + width) << "," << y << "), "
<< "(" << x << "," << (y - heigth) << "), " << "(" << (x + width) << "," << (y - heigth) << ")";
}
main.cpp
#include <iostream>
#include "point.h"
#include "rectangle.h"
#include "rect_funcs.h"
int main() {
Rectangle rect_1, rect_2;
rect_1.makeRectangle(rect_1);
//rect_2 = makeRectangle();
std::cout << "coordintates of rectangle 1: ";
show_Coordinates(rect_1);
return 0;
}
A list of the compiler errors
g++ -c -g -std=c++11 main.cpp
In file included from point.h:4,
from main.cpp:2:
rectangle.h:8:31: error: expected class-name before ‘{’ token
8 | class Rectangle: public Point {
| ^
rectangle.h:12:26: error: expected ‘)’ before ‘,’ token
12 | Rectangle(Point(), int h, int w);
| ~ ^
| )
rectangle.h:14:9: error: ‘Point’ does not name a type
14 | Point getTL() const;
| ^~~~~
rectangle.h:24:9: error: ‘Point’ does not name a type
24 | Point tL;
| ^~~~~
make: *** [makefile:15: main.o] Error 1
➜ Sull_hw3 git:(main) ✗
Aucun commentaire:
Enregistrer un commentaire