Thread: undefined reference to std list during linking

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea how you're reading what you're reading out of what I wrote, but somehow we're not communicating, so we'll let that go.

    The link you posted does not have a person declaring a list in a header file, so that's remarkably irrelevant. (It does have a person creating a class that contains a list inside it -- and look! that person is only declaring a class and not instantiating it! even better/worse, depending on your perspective.)

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by dayalsoap View Post
    "So I agreed that you had to instantiate it differently"

    Then why did you say yes to "I declare it in the header file, I have to instantiate it differently"

    When you say Yes, you are agreeing with two points. And I later discuss "declare in the header file" and you say "just what you said".

    No where, until after that, did you say "you can't declare objects in a header file like this"

    Which is also confusing, because this person declares his list in his headerfile

    Declaring a list in a header file - C++

    "If you know anything about computers, then you yourself know (with or without me telling you) that you can't declare it in the header file."

    C++ != computers.
    What's confusing to me then is why the person has this in his header file

    Code:
    private:
    
    List<int> intergerList1;
    To me, that looks almost identical to what I was doing inside my headerfile, without the private:

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And yet, it isn't. It isn't, because it inside the declaration of a class. Do you know what a class is in C++? If not, please go read what it is. Here might be a good start. A class declaration in C++ is a new declaration of a type like, well, isn't it a class declaration in Java? (I've been trying to burn Java out of my brain, and I've pretty well succeeded.) You are saying "I am defining a new type of object which will contains these objects and these methods" -- but you are not actually making any objects, you are just saying how they will be made when they are needed.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by tabstop View Post
    And yet, it isn't. It isn't, because it inside the declaration of a class. Do you know what a class is in C++? If not, please go read what it is. Here might be a good start. A class declaration in C++ is a new declaration of a type like, well, isn't it a class declaration in Java? (I've been trying to burn Java out of my brain, and I've pretty well succeeded.) You are saying "I am defining a new type of object which will contains these objects and these methods" -- but you are not actually making any objects, you are just saying how they will be made when they are needed.
    It's not in a header file? Pretty sure he posted it in a .h file.

    Your earlier statement said you can't declare things inside header files. You did not add any qualifiers to say "except when it's inside of a class". Unless .h files are not exclusively header files?

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Perhaps it's worth understanding the reasons for the rules tabstop mentions.

    Your program must allocate space for a list exactly once. To do this requires that they be instantiated in exactly one object file (.o), which is compiled from each source file(.c). However, every identifier that is used must be previously declared in the post-processes source.

    With a variable such as a list, this means that you must declare it using exten in a header file, and instantiate it in exactly one source file. With a class, no space is allocated, so there declaration goes in the header, and it might contain lists that use similar syntax to a list definition, but no list with space is actually instantiated.
    Last edited by King Mir; 06-15-2011 at 07:09 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    269
    I think I'm starting to understand. So, without using classes, there isn't really a way to declare it in the header file, but instantiate it in the .cpp file?

    Quote Originally Posted by King Mir View Post
    Perhaps it's worth understanding the reasons for the rules tabstop mentions.

    Your program must allocate space for a list exactly once. To do this requires that they be instantiated in exactly one object file (.o), which is compiled from each source file(.c). However, every identifier that is used must be previously declared in the post-processes source.

    With a variable such as a list, this means that you must declare it using exten in a header file, and instantiate it in exactly one source file. With a class, no space is allocated, so there declaration goes in the header, and it might contain lists that use similar syntax to a list definition, but no list with space is actually instantiated.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dayalsoap
    So, without using classes, there isn't really a way to declare it in the header file, but instantiate it in the .cpp file?
    Err... there is: use extern to declare - but not define - the file scope variable, as in your initial example.

    The point is to avoid breaking the one definition rule: if you only ever include your header file in a single source file, then you can away with defining a variable at file scope in a header. But header files are typically included in more than one source file. Or, you could define the file scope variable in an anonymous namespace, or as static, so that it would be specific to each translation unit (source file + headers) and hence not break the one definition rule when the header is included in multiple source files (but this is clearly not feasible if you want to access the same global variable from multiple translation units).
    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

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by laserlight View Post
    Err... there is: use extern to declare - but not define - the file scope variable, as in your initial example.

    The point is to avoid breaking the one definition rule: if you only ever include your header file in a single source file, then you can away with defining a variable at file scope in a header. But header files are typically included in more than one source file. Or, you could define the file scope variable in an anonymous namespace, or as static, so that it would be specific to each translation unit (source file + headers) and hence not break the one definition rule when the header is included in multiple source files (but this is clearly not feasible if you want to access the same global variable from multiple translation units).
    I think i finally get it now, especially in regards to including the header in multiple files. Thank you so much for your patients. I'm a math person, so I'm not really familiar with this coding stuff. I'm doing this at an Internship at a national lab, and they have a lot of math and analysis libraries.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error: undefined reference
    By somedude in forum C++ Programming
    Replies: 16
    Last Post: 05-21-2009, 03:55 PM
  2. undefined Reference when linking a library
    By steve1_rm in forum C Programming
    Replies: 7
    Last Post: 03-12-2008, 05:34 PM
  3. undefined reference error while linking..help
    By rapture in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 04:54 AM
  4. Linking problem - undefined reference to load_parameters
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:28 AM
  5. Linking Error: Undefined Symbol
    By viclapurga in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2005, 05:45 AM