I apologize for posting such a simple question, but after several tries, I am still confused.
Full disclosure: this is an exercise from "Sams Teach Yourself C++ in 24 Hours" by Jesse Liberty and Rogers Candenhead. This refers to Chapter 9 (Hour 9 Activity 1)
I created a class called Point, in Point.h
I created a class called Rectangle in Rectangle.h and Rectangle.cpp
If I create an int main() function in Rectangle.cpp (that includes Rectangle.h), I can compile Rectangle.cpp and run the resulting program. Fine.
Question:
I create a separate file called main.cpp. I include Rectangle.h. But now the compiler complains.
Code:
$ g++ main.cpp -o main
/tmp/cc38JIph.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `Rectangle::Rectangle(int, int, int, int)'
main.cpp:(.text+0x32): undefined reference to `Rectangle::getArea() const'
collect2: ld returned 1 exit status
If I can create a class in Point.h and use it in Rectangle.h, why can I not just use Rectangle in main.cpp?
I am sure this is a very simple question, I deeply appreciate your pacience and help.
And the files, of course:
file: main.cpp
Code:
#include <iostream>
#include "Rectangle.h"
using std::cout;
using std::endl;
int main() {
//intitialize a local Rectangle variable
Rectangle myRectangle(100, 20, 50, 80);
int area = myRectangle.getArea();
std::cout << "Area: " << area << endl;
std::cout << "Upper left X coordinate: " << myRectangle.getUpperLeft().getX() << endl;
return 0;
}
file: Rectangle.h
Code:
#include "Point.h"
class Rectangle {
public:
Rectangle(int newTop, int newLeft, int newBottom, int newRight);
~Rectangle() {}
int getTop() const {return top; }
int getLeft() const {return left; }
int getBottom() const {return bottom; }
int getRight() const {return right; }
Point getUpperLeft() const {return upperLeft; }
Point getLowerLeft() const {return lowerLeft; }
Point getUpperRight() const {return upperRight; }
Point getLowerRight() const {return lowerRight; }
void setUpperLeft(Point location);
void setLowerLeft(Point location);
void setUpperRight(Point location);
void setLowerRight(Point location);
void setTop(int newTop);
void setLeft(int newLeft);
void setBottom(int newBottom);
void setRight(int newRight);
int getArea() const;
private:
Point upperLeft;
Point upperRight;
Point lowerLeft;
Point lowerRight;
int top;
int left;
int bottom;
int right;
};
file: Rectangle.cpp
Code:
#include "Rectangle.h"
Rectangle::Rectangle(int newTop, int newLeft, int newBottom, int newRight) {
top = newTop;
left = newLeft;
bottom = newBottom;
right = newRight;
upperLeft.setX(left);
upperLeft.setY(top);
upperRight.setX(right);
upperRight.setY(top);
lowerLeft.setX(left);
lowerRight.setY(bottom);
lowerRight.setX(right);
lowerRight.setY(bottom);
}
void Rectangle::setUpperLeft(Point location) {
upperLeft = location;
upperRight.setY(location.getY());
lowerLeft.setX(location.getX());
top = location.getY();
left = location.getX();
}
void Rectangle::setLowerLeft(Point location) {
lowerLeft = location;
lowerRight.setY(location.getY());
upperLeft.setX(location.getX());
bottom = location.getY();
left = location.getX();
}
void Rectangle::setLowerRight(Point location) {
lowerRight = location;
lowerLeft.setY(location.getY());
upperRight.setX(location.getX());
bottom = location.getY();
right = location.getX();
}
void Rectangle::setUpperRight(Point location) {
upperRight = location;
upperLeft.setY(location.getY());
lowerRight.setX(location.getX());
top = location.getY();
right = location.getX();
}
void Rectangle::setTop(int newTop) {
top = newTop;
upperLeft.setY(top);
upperRight.setY(top);
}
void Rectangle::setLeft(int newLeft) {
left = newLeft;
upperLeft.setX(left);
lowerLeft.setX(left);
}
void Rectangle::setBottom(int newBottom) {
bottom = newBottom;
lowerLeft.setY(bottom);
lowerRight.setY(bottom);
}
void Rectangle::setRight(int newRight) {
right = newRight;
upperRight.setX(right);
lowerRight.setX(right);
}
int Rectangle::getArea() const {
int width = right - left;
int height = top - bottom;
return (width * height);
}
file: Point.h
Code:
class Point {
public:
//no constructor
void setX(int newX) { x = newX; }
void setY(int newY) { y = newY; }
int getX() const {return x; }
int getY() const {return y; }
private:
int x;
int y;
};