Thread: beginner's question: simple class usage compiler error.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    beginner's question: simple class usage compiler error.

    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;
    };

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to add Rectangle.cpp to your project or compile like
    Code:
    g++ main.cpp Rectangle.cpp -o main
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    I see, sorry. I knew it must have been something stupid. Many thanks for taking the time and providing a reply so fast.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No question is stupid. As long as you show that are willing and eager to put a little time to solve the problem yourself, you are more than welcome to post trivial questions you cannot grasp.
    In fact, some may even like it when you do since it gives them something trivial to answer (and why do we answer, if not because we want?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question.. wich compiler
    By viper2k in forum C Programming
    Replies: 7
    Last Post: 03-27-2011, 09:19 AM
  2. Beginner question: Error with the compiler
    By d3lilley in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2010, 06:05 PM
  3. newbie question regarding usage of structures in class
    By samual_van in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2005, 10:51 AM
  4. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 08:58 AM
  5. A simple c++ question..Im a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2001, 04:25 PM

Tags for this Thread