Thread: undefined reference to

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    undefined reference to

    I'm learning c++ from the book starting out with c++ and I'm in the chapter of oop. There is an example in the book that I tried to do ,but I get a compile error. the error says undefined reference to 'Rectangle::setLength(double) and it says this error for every function in the class.
    I already checked my code with the book and its exactly the same. Can you help me find the error? there's probably something I'm not seeing. also all of the files are in the same folder and I'm using codeblocks.

    header file
    Code:
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Rectangle
    {
        private:
            double length;
            double width;
        public:
            bool setLength(double);
            bool setWidth(double);
            double getLength();
            double getWidth();
            double getArea();
    };
    #endif
    rectangle.cpp
    Code:
    #include "rectangle.h"
    
    bool Rectangle::setLength(double len)
    {
        bool validData = true;
        if (len >= 0)
            length = len;
        else
            validData = false;
        return validData;
    }
    
    bool Rectangle::setWidth(double w)
    {
        bool validData = true;
        if (w >= 0)
            width = w;
        else
            validData = false;
        return validData;
    }
    
    double Rectangle::getLength()
    {
        return length;
    }
    
    double Rectangle::getWidth()
    {
        return width;
    }
    
    double Rectangle::getArea()
    {
        return length * width;
    }
    main file
    Code:
    #include <iostream>
    #include "rectangle.h"
    using namespace std;
    
    int main()
    {
        Rectangle box;
        double boxLength, boxWidth;
    
        cout << "This program will calculate the area of a rectangle.\n";
        cout << "what is the length? ";
        cin >> boxLength;
        cout << "what is the width? ";
        cin >> boxWidth;
    
        if (!box.setLength(boxLength))
            cout << "invalid box length entered.\n";
        else if (!box.setWidth(boxWidth))
            cout << "invalid box width entered.\n";
        else
        {
            cout << "\nHere is the rectangle's data: \n";
            cout << "Length: " << box.getLength() << endl;
            cout << "width : " << box.getWidth() << endl;
            cout << "Area  : " << box.getArea() << endl;
        }
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to put all the files in the same project, or compile each file and link them together if you're running the compiler yourself.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by tabstop View Post
    You have to put all the files in the same project, or compile each file and link them together if you're running the compiler yourself.
    Thanks put the files in the same project and it worked. would have never figured that out by myself.
    Just a quick question when I added the files to the project it says select the targets this file should belong to. there is 2 options debug and release. which one do I select both or just one?? I selected both and it worked, but I'm wondering if it makes a difference what you select.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Debug means "this is something you can debug", while Release means "this is something you can release to other people".

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    8
    ohh ok. thanks for the help tabstop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM