Linker Error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Linker Error

  1. #1
    Join Date
    Feb 2009
    Posts
    5

    Linker Error

    First, I'll go ahead and thank any of you who can help me. Godspeed.

    Second, I'll give some small background which may or may not be a part of my problem.

    I'm a college student in the entry level C++ course. I've had two semesters of Java and a lot of practice programming. The concepts aren't lost on me, but a lot of the syntax and terms are for C++.

    Now, to the problem:

    I've spent the past few days working on the first assignment for my course. Essentially it involves creating a list of assignments. Each assignment is an object and each object has two pieces of data; A name, and a due date.

    I'm using Dev-C++ 4.9.9.2 and there are 5 files in my project. I've managed to get it to compile without any of the normal issues I run into, typically syntax errors. Now I'm getting the dreaded "[Linker error] undefined reference to 'class::function()' type error.

    Specifically, the error Dev-C++ is giving me is

    [Linker error] undefined reference to `assignment::getDue()'
    [Linker error] undefined reference to `assignment::getName()'
    [Linker error] undefined reference to `assignment::getDue()'
    [Linker error] undefined reference to `assignment::getName()'
    [Linker error] undefined reference to `assignment::getDue()'
    [Linker error] undefined reference to `assignment::getName()'
    [Linker error] undefined reference to `assignment::getName()'
    [Linker error] undefined reference to `assignment::getName()'
    Id returned 1 exit status
    [Build Error] [Assignment1.exe]Error1
    I understand what that error is and I've had it before. Usually it's because I forgot to put a ; at the end of my header file or forgot to include it in my .cpp file. I've been combing through my code for a little over an hour now and I just can't figure this one out. And here we are.

    Now, for the code!

    assignment.h
    Code:
    #ifndef ASSIGNMENT_H
    #define ASSIGNMENT_H
    
    #include <iostream>
    #include <list>
    #include <string>
    using namespace std;
    
    class assignment
    {
          public:
                
                 
                 assignment(string name, string due);
                 
                 string getName();
                 string getDue();
                 
                 string assName, assDue;
    
    };
    #endif
    assignment.cpp
    Code:
    #include "assignment.h"
    
    using namespace std;
    
    string assName, assDue; 
    
    assignment::assignment(string name, string due)
    {
       assName = name;
       assDue = due;
    }
    
    string getName()
    {
           return assName;
    }
    
    string getDue()
    {
           return assDue;
    }
    assList.h
    Code:
    #ifndef ASSLIST_H
    #define ASSLIST_H
    #include "assignment.h"
    #include <iostream>
    #include <list>
    #include <string>
    using namespace std;
    
    
    class assList
    {
                 
          public:
                 assList();
                 //create list
                 
                 void add(assignment ***);
                 //Add a new assignment to the list
                 
                 void remove(string myName);
                 //Remove an assignment from the list
                 
                 void printList();
                 //Cout the list in the order they were input
                 
                 void find();
                 //Find the assignment with the earliest due date.
                 
                 list<assignment> myList;
    };
    #endif
    assList.cpp
    Code:
    #include "assList.h"
    #include "assignment.h"
    #include <list>
    #include <string>
    
    
    using namespace std;
    
    assList::assList()
    {
       list<assignment> myList;
    }
    
    void assList::add(assignment ***)  
    {
       myList.push_front(***);
    }
    void assList::find()
    {
         list<assignment>::iterator date;
         date = myList.begin();
         assignment temp = *date;
         
         string earliest = temp.getDue();
         string name = temp.getName(); 
         while(date != myList.end())
         {
            date++;
            assignment nextTemp = *date;
            
            string nextDate = nextTemp.getDue();
            string nextName = nextTemp.getName();
            if(nextDate < earliest)
            {  
                    earliest = nextDate;
               name = nextName;
            }
         }
         cout<<name<<" is due the earliest on : "<<earliest;
    }
    void assList::printList()
    {
         list<assignment>::iterator printiter;
         printiter = myList.begin();
         assignment temp = *printiter;
         while(printiter != myList.end())
         {
              temp = *printiter;
              cout<<temp.getName()<<" "<<temp.getDue()<<"\n";
              printiter++;
         }
    }
                  
    void assList::remove(string myName)
    {
         list<assignment>::iterator front,current, back;
         front = myList.begin();
         current = front;
         back = myList.end();
         back--;
         bool deleted = false; 
         assignment temp = *current;
         string currentName = temp.getName();
         if(currentName == myName)
         {
            myList.erase(current);
            deleted = true;
         }
         else
         {    
              while(current != myList.end())
              {     
                    current++;
                    temp =  *current;
                    currentName = temp.getName();
                    if(currentName == myName)
                    {
                                   myList.erase(current);
                                   deleted = true;
                    }
              }
         }
         if(deleted)
             cout<<myName<<" was found and deleted."<<"\n";
         else
             cout<<myName<<" was not found!."<<"\n";
    }
    main.cpp
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <list>
    #include "assignment.h"
    #include "assList.h"
    
    using namespace std;
    
    int main()
    {
        assList List;
        
        assignment a1("Assignment1", "Feb 20, 2009");
        List.add(a1);
        
        assignment a2("Assignment2", "Feb 25, 2009");
        List.add(a2);
        
        assignment a3("Assignment3", "Feb 17, 2009");
        List.add(a3);
        
        assignment a4("Assignment4", "Feb 21, 2009");
        List.add(a4);
        
        assignment a5("Assignment5", "Feb 19, 2009");
        List.add(a5);
        
        List.printList();
        system("PAUSE");
        
        List.find();
        system("PAUSE");
        
        List.remove("Assignment3");
        system("PAUSE");
        
        assignment a6("Assignment6", "Feb 18, 2009");
        List.add(a6);
        
        List.printList();
        system("PAUSE");
        
        List.find();
        
        system("PAUSE");
        return 0;
    }
    Again, thank you guys for any help you can give me.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Linker Error

    Quote Originally Posted by Will.Holland View Post
    I'm a college student in the entry level C++ course. I've had two semesters of Java and a lot of practice programming.
    All I will say about that is that Java is not C++. Pretend you don't know Java when programming C++, as anyone who tries to use Java techniques when programming C++ will make a mess of things.

    Second, those errors are not compiler errors, those are linker errors. The linker is responsible for taking object code and resolving the functions that are being called. What that error is telling you is that you're calling those functions, but where are they?

    When you compile a C++ application, you must compile all of the source modules seperately, and then the linker takes these compiled modules (object code) and ties them together to create the final executable. The linker is responsible for actually figuring out where these functions you're calling reside.

    More than likely, you did not put together your project correctly, as your project consists of multiple .cpp source files. You probably only compiled one of those source files, and then told the linker "go ahead and create the executable". That is when the linker said back to you "these functions cannot be found". You need to compile main.cpp, assignment.cpp, and asslist.cpp thus creating 3 object files. Then the linker must take these 3 object files and create the executable. You are obviously not doing this.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2009
    Posts
    5

    Re: Linker Error

    Thanks for the reply.

    I knew what my compiler was trying to tell with the linker error, I just don't understand why I'm getting the error.

    Using Dev-C++ is fairly simple. Create a project, add the files to it, choose rebuild all, and it works.

    Just to be sure though, I created a new project, added all 5 files to the project and tried again. Same exact error.

    I checked the directory where I'm saving these files, and there is a .o file for each of my 3 .cpp files.

    Been going over my code some more though, and I notice that the numbers and the order of the errors directly coincides with the code I have in my assList.cpp file where I call temp.getDue(); temp.getName(); nextTemp.getDue(); and nextTemp.getName();

    I'm guessing that it has something to do with that.

    Again, thanks for the response.

    ~Will

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Linker Error

    Code:
    assignment::assignment(string name, string due)
    string getName()
    string getDue()
    assList::assList()
    void assList::add(assignment ***)  
    void assList::find()
    void assList::printList()
    void assList::remove(string myName)
    Two of these things are not like the others, six of these things are kinda, the same. Two of these things are not like the others, and now it is time---to play our game!

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Linker Error

    Quote Originally Posted by Will.Holland View Post
    Thanks for the reply.

    I knew what my compiler was trying to tell with the linker error, I just don't understand why I'm getting the error.

    Using Dev-C++ is fairly simple. Create a project, add the files to it, choose rebuild all, and it works.

    Just to be sure though, I created a new project, added all 5 files to the project and tried again. Same exact error.
    Always try a simple test first.
    Code:
    // main.cpp
    
    void foo();
    
    int main()
    {
       foo();
    }
    Code:
    #include <iostream>
    // foo.cpp
    void foo()
    {
       std::cout << "Foo called";
    }
    Here are two files. Create a project. Compile and link. Does it work? If not, then there is something wrong with your handling of the Dev-C++ environment, and you need to sort those problems out first before proceeding.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Feb 2009
    Posts
    5

    Re: Linker Error

    Paul,

    That code works flawlessly.

    Lindley, let me study that for a bit.

  7. #7
    Join Date
    Feb 2009
    Posts
    5

    Re: Linker Error

    Two of these things are not like the others, six of these things are kinda, the same. Two of these things are not like the others, and now it is time---to play our game!
    HAHA! Of course!

    Well that solves that issue. It will compile now. Now it crashes when it tries to execute List.printList();

  8. #8
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Linker Error

    The real problem is that this:
    Code:
    string getName()
    {
       // whatever
    }
    However, it should be this:
    Code:
    string assignment::getName()
    {
       // whatever
    }
    If you would have gotten rid of those global variables, you would have spotted this at compile time:
    Code:
    string assName, assDue;
    Why are these two variables declared in assignment.cpp? They are not the same variables as the member variables in the assignment class.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Linker Error

    Quote Originally Posted by Will.Holland View Post
    HAHA! Of course!

    Well that solves that issue. It will compile now. Now it crashes when it tries to execute List.printList();
    Code:
    printiter = myList.begin();
    assignment temp = *printiter;
    What would happen if the list is empty? You're dereferencing an invalid iterator.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Linker Error

    Also, what is "***"?
    Code:
    void assList::add(assignment ***)  
    {
       myList.push_front(***);
    }
    Do not hide the real code. How you pass parameters is just as important as the other parts of the code.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Feb 2009
    Posts
    5

    Re: Linker Error

    Paul,

    In regards to my global variables, they are unnecessary. The reason for them in the first place was just me troubleshooting some earlier issue and--once I added those global variables--it stopped being an issue. Just so happened it just a 'bandaid' to get me to the larger issue.

    As far as dereferencing a pointer, again, you are right. When I was writing the code for this assignment I was keeping in mind the scope of what I would be doing in main.cpp. I didn't include the necessary NULL check in there because, in this particular example, I knew it would never be null.

    Finally, no one hid the parameter, it was filtered by the forums I imagine. I hadn't realized it until Lindley made his post, but that particular parameter is the first three letters in assignment.

    The issues I had with the crashing were from me pointing my iterators to a position just beyond the size of my list. Once I figured that out, the program runs now.

    All that's left to do now is to trim the fat.

    Thank you guys so much for your help.

    ~Will

  12. #12
    Join Date
    Aug 2007
    Posts
    858

    Re: Linker Error

    Quote Originally Posted by Paul McKenzie View Post
    Also, what is "***"?
    Code:
    void assList::add(assignment ***)  
    {
       myList.push_front(***);
    }
    Do not hide the real code. How you pass parameters is just as important as the other parts of the code.

    Regards,

    Paul McKenzie
    I would guess that it was a-s-s without the '-'s and got caught by the forum filter.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center