Thread: Problem: [Linker error] undefined reference to ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Problem: [Linker error] undefined reference to ...

    I's using dev c++ to compile my program and i get a linker error for every class function that main calls.

    I included the code for the .cpp and .h files in a .txt file called "code", i also included the .txt file that the program needs to read, "prefixRanges".

    Any one know what i did wrong here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you added all the .cpp files to your project?
    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.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Yeah i added everything you'd need to compile it in the code.txt, i probably should've bolded the headers that indicated a new .h or .cpp file

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I noticed that your Order.cpp forward-declares ISBN and then goes on to use it as if it was fully defined. This shouldn't even compile.

    You need to include Isbn.h in Order.cpp instead.

    Your headers are also missing header guards (to guard against multiple inclusion from the same compilation unit).
    Code:
    #ifndef ISBN_H
    #define ISBN_H
    
    //header contents here
    #endif
    It is also considered bad style to use using directive in headers (files including that header get the std:: namespace whether they want it or not).

    By the way, ISBN's are nowadays 13-digit (not 10-digit) if I'm not mistaken
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    made the follwing changes:
    order.cpp:
    Code:
    #ifndef Order_H
    #define Order_H
    #include "Order.h"
    #include "ISBN.h"
    #endif
    isbn.cpp:
    Code:
    #ifndef ISBN_H
    #define ISBN_H
    #include "ISBN.h"
    #include "ISBNPrefix.h"
    #endif
    isbnprefix.cpp
    Code:
    #ifndef ISBNPrefix_H
    #define ISBNPrefix_H
    #include "ISBNPrefix.h"
    #endif
    also changed the class ISBN; in order.cpp

    only thing i'm stuck on is the "using directive in headers", don't know what that is exactly

    btw, i'm still getting the linker errors....and yes they are 13, but our prof said we'll only be using 10 digits

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, header guards go into headers.

    Generally it is not recommended to put using namespace ... into headers. You might rather prefix all the few standard things you have there (like ostream) with std:: (std:stream etc).

    ----

    Well, I tried to split it into a project and compile it, but I get several compiler errors before I can even get to linking.

    Firstly, forward declarations normally go into headers to avoid including the header where the full definition is found. If you access members and methods of these classes in the implementation file, a forward declaration is not enough. You need to include all headers in implementation files that the cpp file needs to know about.

    Secondly, FILE* fp is a private member in ISBNPrefix, yet you are trying to access it directly from ISBN.

    ISBN::empty has a malformed if-condition (caught apparently because the method is const - see where that helps!):
    Code:
    if(ISBN1[0] == '\0' && area[0] == '\0' && publisher[0] == '\0' && title[0] == '\0' && isRegistered = 0)
    ISBN::registered appears to contain a stray }

    Code:
    void ISBN::toStr(char* str) const{
      /* *MAKE UNFORMATTED VERSION OF ISBN* */
      strcpy(str,isbn);
    }
    isbn is undefined (it is not a member, not a global and obviously not a local variable).

    And the errors go on and on... (What you posted cannot be the same code that you compiled successfully and cannot link.)

    There are also numerous warnings: unused variables and functions that should return something but don't.
    Last edited by anon; 07-19-2008 at 12:33 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Or in other words, since you say you are getting linker errors it means that you must be compiling only main.cpp and not any of the other files.

    So again, have you created a project with Dev-C++ and added all the cpp files to it? You can compile single-file programs without a project but you need one if you have more files.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    damn, thats alot of problems...how can i use ISBNPrefix's fp because without it this program is useless seeing i need to rewind and read from fp

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by anon View Post
    Or in other words, since you say you are getting linker errors it means that you must be compiling only main.cpp and not any of the other files.

    So again, have you created a project with Dev-C++ and added all the cpp files to it? You can compile single-file programs without a project but you need one if you have more files.
    yeah, thats what i've been doing so im looking through and correcting any silly mistakes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. problem creating .so
    By k0k33 in forum C Programming
    Replies: 7
    Last Post: 04-27-2009, 04:41 AM
  3. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. Undefined Reference Problem
    By esilja in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2001, 09:05 AM