[Templates] I swear they hate me.

This is a discussion on [Templates] I swear they hate me. within the C++ Programming forums, part of the General Programming Boards category; I can't seem to get these to link lately. Code: /* CheckAlloc * A convenience function to save writing the ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    [Templates] I swear they hate me.

    I can't seem to get these to link lately.

    Code:
    /*  CheckAlloc
     *  A convenience function to save writing the same "check pointer" code
     *  after having attempted to allocate memory through new.
     */
    template <class T> void CheckAlloc( T* ptr )
    {
        if ( !ptr )
            ErrorLogging::Log( "CheckAlloc: Failed - memory exhausted?", true );
    }
    (Feel free to rip at the idea of this CheckAlloc thing - I need feedback!)

    I get this link error (good ol' 2019):

    Code:
    Cvars.obj : error LNK2019: unresolved external symbol "void __cdecl CheckAlloc<struct Cvar>(struct Cvar *)" (??$CheckAlloc@UCvar@@@@YAXPAUCvar@@@Z) referenced in function "struct Cvar * __cdecl MakeCvar(unsigned long,long,float,double,unsigned char,bool,char *)" (?MakeCvar@@YAPAUCvar@@KJMNE_NPAD@Z)
    If I defined that, wouldn't I be specialising?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Unless you are using an old, non-standard compiler, new will throw an exception if it fails to allocate memory, so you don't need to check for null anyway. A better way might be to overload new to do what you want.

    As far as the linker error, as long as that function is defined in a header included by the source file that defines MakeCvar, it should work.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ah I see. Thankies.
    I'm uncomfortable with try and catch blocks. I always end up using them "C-style" and it ends up making a terrible mess. Observe:

    Code:
    try
    {
        // this
    }
    catch ( /* whatever*/ )
    {
        // handle it
    }
    ^ For every block of code that could fail.

    Actually I recall something that could help, new_handler or something (?). Would that be a better solution?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    There is a std::set_new_handler function. I don't know much about it, though
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    God bless MSDN Jan 2004.


    (I need to update. I could just use the online one, but F1 is more convenient than click click click type type typesomemore).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    You could use nothrow new. That returns NULL instead of throwing an exeption.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 10:58 PM
  2. I hate being computer literate
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 10-14-2002, 11:47 AM
  3. Pet Peeves
    By Srg Pepper in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 10-03-2002, 11:34 AM
  4. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 06:30 PM

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