Thread: Arghhhh! LNK 2005 error.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Arghhhh! LNK 2005 error.

    I am at wits end. I was just trying to rejigger the placement of functions in headers and source files so that it would seem more organized, and the code compiled fine before that, but now it keeps throwing me a bunch of LNK 2005 errors and I can't revert to the state it was in before.

    Code:
    ot_ok error LNK2005: "int __cdecl pis::set_open_back_unrounded(class phone &)" (?set_open_back_unrounded@pis@@YAHAAVphone@@@Z) already defined in main.obj
    ot_ok error LNK2005: "int __cdecl pis::set_close_mid_front_unrounded(class phone &)" (?set_close_mid_front_unrounded@pis@@YAHAAVphone@@@Z) already defined in main.obj
    
    ...
    
    and on and on
    So it gives me a total of 62 errors. All of them from a group of functions I placed in the namespace "pis" under the header "phone_initialize.h". It worked fine before and I have no idea why it's giving me grief now.

    I can't isolate the problem so unfortunately, I'm going to have to attach the entire project. The attachment file is a .zip archive so you'll have to change the extension to .zip.

    Any help at all would be truly appreciated (not that I didn't appreciate it before, but this is on a different scale).
    Last edited by cunnus88; 02-04-2008 at 01:12 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I didn't look at your zip, but those errors are usually either because you accidentally copied the definition of those functions into multiple places, or because you implemented the functions inside of a header file.

    Where do you define (not declare) the set_open_back_unrounded(class phone &) function?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Ohmegosh, it worked. Thank you, thank you, thank you.

    But I don't get it.

    Originally my functions were placed in a namespace of header called "phone_initialize.h".
    Code:
    namespace pis
    {
    	void set_whatever_a(argument){body;}
    	void set_whatever_b(argument){body;}
    
    	and on and on
    }
    They compiled before, but I fooled around with the placement of some stuff that used these functions and then it gives me external link errors. What I actually did was, I abstracted some bit of code that I had in main and placed it in some other source file.

    So I took your advice and completely removed the functions under the namespace pis and moved it into the source file "phone_initialize.cpp".

    It compiled but now I'm worried. I mean, there are no function declarations in the original header but the compiler is still finding the function definition and compiling it. Shouldn't I have at least left the declarations in the header?

    Oh, well, I'll save that for a later head-racking.

    Again, thank you, merci, danke schoen, gracias, takk, arigato, gamsahabnida, and a thousand other thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The namespace has nothing to do with it. You placed the definitions inside a header file, which is wrong. The solution is to put the definitions inside a namespace in the cpp file:
    Code:
    //header
    
    namespace pis
    {
        // prototype
        void set_whatever_a(argument);
    }
    Code:
    // source
    
    namespace pis
    {
        // definition
        void set_whatever_a(argument){body;}
    }
    You can put things into a namespace from as many different files as you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM