Thread: Help! Undefined Reference

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    7

    Help! Undefined Reference

    Hi guys.
    I am current facing an annoying problem. I have written the following classes:
    Code:
    #ifndef _EMPLOYEE_H
    #define _EMPLOYEE_H
    
    #include <string>
    
    using namespace std;
    
    class Employee {
    public:
      Employee(string theName, float thePayRate);
    
      string getName() const;
      float getPayRate() const;
    
      float pay(float hoursWorked) const;
    
    protected:
      string name;
      float payRate;
    };
    
    #endif /* not defined _EMPLOYEE_H */
    Code:
    #include "employee.h"
    
    using namespace std;
    int main(){}
    
    Employee::Employee(string theName, float thePayRate)
    {
      name = theName;
      payRate = thePayRate;
    }
    
    string Employee::getName() const
    {
      return name;
    }
    
    float Employee::getPayRate() const
    {
      return payRate;
    }
    
    float Employee::pay(float hoursWorked) const
    {
      return hoursWorked * payRate;
    }
    Code:
    #ifndef _MANAGER_H
    #define _MANAGER_H
    #include <string>
    #include "employee.h"
    
    using namespace std;
    
    class Manager : public Employee {
    public:
      Manager(string theName,float thePayRate,bool isSalaried);
    
      bool getSalaried() const;
    
      float pay(float hoursWorked) const;
    
    protected:
      bool salaried;
    };
    
    #endif /* not defined _MANAGER_H */
    Code:
    #include "manager.h"
    
    using namespace std;
    
    int main(){}
    Manager::Manager(string theName,float thePayRate,bool isSalaried): Employee(theName, thePayRate)
    {
      salaried = isSalaried;
    }
    
    bool Manager::getSalaried() const
    {
      return salaried;
    }
    
    float Manager::pay(float hoursWorked)
    {
      if (salaried)
        return payRate;
      /* else */
      return Employee::pay(hoursWorked);
    }
    I have tried to compile manager.cpp many times in Dev C++ 4.9.9.2 but the message are always:
    Code:
      [Linker error] undefined reference to `Employee::Employee(std::string, float)' 
      [Linker error] undefined reference to `Employee::Employee(std::string, float)' 
       [Linker error] undefined reference to `Employee::pay(float) const' 
      ld returned 1 exit status
    Can anyone help me?Thank you in advance =)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you did not link the object file generated from the source file with the definition of the Employee constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Location
    Antwerp, Belgium
    Posts
    12
    I've got a similar problem,
    How do you link them?

    cheers, Carola

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, if you are using Dev C++ 4.9.9.2 then you should create a project and place your files in that project. The same applies to some other IDEs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Should I put it as a Window Application, Console Application or an Empty Project.
    And how to link employee class and manager class.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Either Console Application or an Empty Project would probably be correct for you. The IDE would get the linking done for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    OMG, I can do it now. Thank you very much laserlight.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Location
    Antwerp, Belgium
    Posts
    12
    I'm using Code::Blocks with MinGW,
    Everything's within one project already,
    but for some reason it doesn't work.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #ifndef _EMPLOYEE_H
    #define _EMPLOYEE_H
    
    
    #ifndef _MANAGER_H
    #define _MANAGER_H
    From C-FAQ:
    The rules, paraphrased from ANSI Sec. 4.1.2.1, are:

    * 1. All identifiers beginning with an underscore followed by an upper-case letter or another underscore are always reserved (all scopes, all namespaces).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Carola View Post
    I'm using Code::Blocks with MinGW,
    Everything's within one project already,
    but for some reason it doesn't work.
    Then there may be another reason. Perhaps you haven't implemented the functions? Code?
    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.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think it's safe to say that both of your source files are not getting linked. If they were, you would be getting an error complaining that main() has been defined more than once. You have main() implemented in employee.cpp and manager.cpp.
    bit∙hub [bit-huhb] n. A source and destination for information.

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