Having trouble with llinker and Undefined reference errors

This is a discussion on Having trouble with llinker and Undefined reference errors within the C++ Programming forums, part of the General Programming Boards category; Im having difficulty compiling this program. I wrote a .h file that includes the class, constructor, mutator, and accessor functions-- ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    9

    Having trouble with llinker and Undefined reference errors

    Im having difficulty compiling this program. I wrote a .h file that includes the class, constructor, mutator, and accessor functions-- a .cpp implementation file that includes the definitions of the functions, and the main .cpp program that tries to utilitze these functions. I've been trying to debug and have not found a solution. I broke down the problem into a simple task just to see if it will work but i still receive the linker error. The final program will have a derived class and its implementation file but i want to try to work in out in sections. In this example, im just trying to get it to work so im just using a single character for the employee name and just integers for the date ,not trying to get the whole string.Here's what i have as of yet:

    employee.h
    Code:
    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H
    
    class Employee
    {
          private:
                  char EmployeeName;// To hold the employee name
                  int EmployeeID;// To hold the employee ID
                  int HireDate;// To hold the employee hire date
          public:
                 // constructor 1
                 Employee();
                 // constructor 2
                 Employee(char,int,int);
                 //Mutator function
                 void setName(char);
                 void setID(int);
                 void setDate(int);
                 //Accessor function
                 const char GetName() const
                 {return EmployeeName;}
                 int GetID() const
                 {return EmployeeID;}
                 int GetDate() const
                 {return HireDate;}
                 };
    #endif
    employee.cpp(implementation file that has the function definitions)
    Code:
    #include "Employee.h"
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    // The constructor function definition
    Employee::Employee()
    {
        EmployeeName = '\0';
        EmployeeID = 0;
        HireDate = 0;
     }
    
    Employee::Employee(char n, int i,int d)
    {
        strcpy(EmployeeName, n);
        EmployeeID = i;
        HireDate = d;
    }
    //Mutator Definitions
    void Employee::setName(char n)
    {
        strcpy(EmployeeName,n);
    }
    //------------
     void Employee::setID(int i)
    {
        EmployeeID = i;
    }
     //-----------
     void Employee::setDate(int d)
     {
        HireDate = d;
    }
    mainprogram.cpp (The main program file)
    Code:
    #include<iostream>
    #include<cstring>
    #include "Employee.h"
    using namespace std;
    int main()
    {
    int i,d;
    char n;
    cout<< "enter a letter";
    cin >> n;
    cout << "enter your ID";
    cin >> i;
    cout <<"enter your date";
    cin >> d;
    
    Employee pay(n,i,d);
    cout << pay.GetID();
    
    system("pause");
    return 0;
    }
    I've only started learning the basics of c++ since last fall. Its becoming more challenging as i progress and would very much appreciate any assistance from the community. Thank you

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    it looks good. Have you compiled and run it? what are the specific errors that you encountered?
    Last edited by nimitzhunter; 04-06-2011 at 07:47 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    yea, everytime i try to compile i get this error
    Linker error- undefined reference to 'Employee::Employee(char,int,int)'
    Id returned 1 exit status

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Then look at how your ide compile multiple files and linking.

    Beyond that, there is the error due to the wrong usage of strcpy. Neither employeename or 'n' is char* which is the arguments type of strcpy. And Employee is not even a C-string so there is no point using strcpy. Just use a simple assignment for your example.
    Code:
    n = EmployeeName;
    Last edited by nimitzhunter; 04-07-2011 at 12:21 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by odaness View Post
    yea, everytime i try to compile i get this error
    Then your program isn't being linked against employee.cpp. If you're compiling from the command line, you should be doing something like:

    Code:
    g++ mainprogram.cpp employee.cpp
    but if you're doing this:

    Code:
    g++ mainprogram.cpp
    then you should expect to get that linker error.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    @hunter thanks for pointing out about the strcpy, Ill change that.
    @mozza314, hey thanks for the help with the sort functions from before. It works like a charm now. Im using the Bloodshed Dev c++ compiler, no linux. When i compile, i click on an icon in the Dev c++ application where i write the code and it attempts to compile it but it gives me a linker error with
    Linker error- undefined reference to 'Employee::Employee(char,int,int)'
    Id returned 1 exit status

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You could add a
    Code:
    #include "employee.cpp"
    at the end of the header file(or in the main file) to get around this..!
    but (I don't know why), that is considered a bad practice. <- (## Could anyone of you clarify about it, please ?)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by odaness
    Im using the Bloodshed Dev c++ compiler, no linux. When i compile, i click on an icon in the Dev c++ application where i write the code and it attempts to compile it but it gives me a linker error with
    Perhaps you forgot to add this source file to the Dev-C++ project.

    Quote Originally Posted by manasij7479
    at the end of the header file(or in the main file) to get around this..!
    but (I don't know why), that is considered a bad practice. <- (## Could anyone of you clarify about it, please ?)
    If you #include "employee.cpp" in a header, then the various functions defined in that source file would be defined each time the header is included, breaking the one definition rule. If you place the include in another source file, that's fine, except that then someone who comes along and decides to compile both source files and link them would get a multiple definition error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    I think i have the proper head files in place...typically just having --#include "Employee.h"--should make it work right..or is there something else im missing...I shouldn't have to download anything extra for the dev c++ compiler.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    I would say you need to drop the char (they only hold a single character!) and use std::string.
    Your indentation could also be improved somewhat.
    Also, know that Dev-C++ is somewhat outdated. If at all possible, you should use a newer IDE/compiler (http://sourceforge.net/apps/mediawik....php?title=IDE).
    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
    Apr 2011
    Posts
    9
    so pretty much the code i've written is not causing the linker error:
    Linker error- undefined reference to 'Employee::Employee(char,int,int)'
    rather its dev c++ that im using is whats not working properly?

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > rather its dev c++ that im using is whats not working properly?
    Well, your inability to set up a project containing more than one source file.

    At a rough guess, it would be something like project->settings->source->add source
    Anyway, somewhere down there, you'll find "mainprogram.cpp", so add your other files to that list.

    > but (I don't know why), that is considered a bad practice. <- (## Could anyone of you clarify about it, please ?)
    Unless you are very careful, you run the risk of moving from "missing definition" to "multiple definition" because you somehow managed to include the same source file twice.
    The other main reason is that you end up recompiling ALL the code ALL the time, even when you make only trivial changes.

    A well defined IDE project (or more traditional makefile) would only recompile the changed source file, then relink the executable.

    It would be a quick fix for a small number of files, but as your projects grow into 10's or 1000's of files, #including source code becomes totally unmanageable.

    There is one exception to this however, and that is where your .cpp file contains ONLY inline functions and/or template functions. But you might be better off placing such things in .h files, even though they contain "source code".
    Edit: oh, now I've seen laserlight's answer as well - oh well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    multiple definitions
    Can't preprocessor guards be put to avoid that ?..although I'd admit that I could not set them up precisely..and every time I tried, I ended up deleting them..!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by manasij7479
    Can't preprocessor guards be put to avoid that ?
    No, because the multiple definitions are across translation units. Your header inclusion guards only prevent those definitions from happening more than once in the same translation unit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    What is a translation unit ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21