DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: how to solve "error LINK2005"?

  1. #1
    Join Date
    Mar 2010
    Posts
    61

    how to solve "error LINK2005"?

    hey
    i'm writing a program at the end VS give this errors to me,what are these and what can i do to solve these?
    PHP Code:
    Error    1    error LNK2005"class display myDisplay" (?myDisplay@@3Vdisplay@@Aalready defined in disks.obj    exerecise.obj
    Error    2    error LNK2005
    "class disks myDisks" (?myDisks@@3Vdisks@@Aalready defined in display.obj    exerecise.obj
    Error    3    error LNK2005
    "struct _COORD coordinate" (?coordinate@@3U_COORD@@Aalready defined in display.obj    movement.obj
    Error    4    error LNK2005
    "void * hndl" (?hndl@@3PAXAalready defined in display.obj    movement.obj
    Error    5    error LNK2005
    "class disks myDisks" (?myDisks@@3Vdisks@@Aalready defined in display.obj    movement.obj
    Error    6    error LNK2005
    "class pegs myPegs" (?myPegs@@3Vpegs@@Aalready defined in movement.obj    pegs.obj
    Error    7    error LNK2005
    "class disks myDisks" (?myDisks@@3Vdisks@@Aalready defined in display.obj    pegs.obj
    Error    8    error LNK2005
    "class movement myMovement" (?myMovement@@3Vmovement@@Aalready defined in exerecise.obj    pegs.obj
    Error    9    fatal error LNK1169
    one or more multiply defined symbols found    C:\Users\JJ25\Documents\Visual Studio 2008\Projects\exerecise\Debug\exerecise.exe    1 
    thanks in advance

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    Most likely You have no include guards on one of your headers.

    Include guards look like this:

    #ifndef _myheader_h
    #define _myheader_h

    header file contents

    #endif

    Not having this means that if you #include the same header twice, its multiply defined.
    Last edited by jonnin; 04-30-2010 at 07:54 AM.

  3. #3
    Join Date
    Mar 2010
    Posts
    61
    thanks a Lot,since I've not read this part of c++ programming yet,i'm not sure it can solve this problem(but i know what those are)
    i write a program which have 4-5 class and every class has a communicate to other classes by an object of other class, i strongly need to use each objects that have been created
    from several classes in every class, i came and put objects in global of program
    stdafx.h header file
    which include in every class so i can use them and gain information which saved in this objects
    so, i want to know if there is other way to reach this goal(of course there is some,but how?)?
    or the answer that you gave me can solve this matter?
    note that the
    Code:
    display myDisplay
    movement myMovement
    pegs myPegs
    disks myDisks
    are my objects which shown in error list

  4. #4
    Join Date
    Mar 2010
    Posts
    61
    and also it did not work

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    stdafx implies precompiled headers which, well they are precompiled and visual forgets to fix them after an update, keeping old object files from old compiles and mixing them with new object files from your current compile and making a mess. Its usually best to turn that stuff off and remove stdafx from your project entirely if you think you can manage it -- you have to manually include the proper windows headers if you do that and it may be too much to unravel at this time. If you want to precompile something, though, make a library. Automated precompile and second guessing is just that, a guess.

    Still, its telling you what the problem is. Why does the compiler think you have 2 things with the same name? Either you really do, like this:

    int x;
    double x; //error, multiply defined symbol....

    but thats a compiler error, and yours is at the linker stage.

    Because its at the linker stage, it has to be from broken include guards, so that somewhere a header file is included more than once which makes 2 copies of some entity which breaks things.

  6. #6
    Join Date
    Mar 2010
    Posts
    61
    so in brief ,how can i solve this problem?(i removed stdafx.h and handled every thing by myself but it didn't work, and the compiler gave me the same error)
    note that by removing stdafx.h (again) i create the each object in classes which i going to work with them and compiler gave me the errors that you "class example object" already defined in "sth else.obj" when i clean those object then compiler didn't know the objects in that class which i work with them and gave me a lot of error.

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    post example.h

  8. #8
    Join Date
    Mar 2010
    Posts
    61
    all my problem is, i want to declare 5 object which entire project can use them and their information
    and in other forums some people tell me "use extern objects" how you suggest that?
    and how can i use or write extern objects or variable?
    because of this(using in entire project every 5 object)i put the variable on the stdafx.h which include in every class and compiler see objects as it included i.e for 5 class we have 5
    Code:
    #include "stdafx.h"
    compiler see each object 5 times(!)
    and there is not only a class there is 5 class,"example.h "was just for example
    if you need i can send classes
    and also thanks for your patient.

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    extern is a keyword used to make global variables. This is a mess and a poor way to solve a problem, but this is how you do it:

    in one master file you have
    whatever y;

    and in any file that needs to see this variable, you have:
    extern whatever y;

    which tells the linker that the defination for y is not here, its somewhere else, but you want to use it.

    ----------------------

    Now, about that, lets take a look at a decent way to handle this.
    in your main file

    whatever vars[5];
    whatever *getglobals(); ///this function goes into a .h file that all other files can include!
    //only this one line above! Call that file my5globals.h
    whatever *getglobals
    {
    return vars;
    }

    now, in any old function that needs to get to your global data:

    #include <my5globals.h>
    whatever * getat = getglobals();
    getat[0].field = whatever;
    getat[2].otherfield *= 3;
    etc.

    I prefer this to extern, but you can do it either way. Extern is beyond the control of the programmer. Here, you routed the globals through a function call and, if you wanted to get sophisticated, you could do this with static members of a class to really clean it up.


    Globals are always a problem, they create all sorts of new ways to create bugs in a program as well as make it difficult to follow the code. The more layers of abstraction you can poke around them, the better the code will look and feel. Even if the values really are global, by forcing the user to call a function like I did, where the name of the function tells you the memory is global, you at least create awareness of the problem while encapsulating them somewhat, and with a class that does the same thing, you can add a LOT of protection to your globals to defend them from unwanted side effects and the other issues caused by raw globals.

  10. #10
    Join Date
    Mar 2010
    Posts
    61
    many thanks, it's working now(finally)

Similar Threads

  1. Replies: 3
    Last Post: 05-21-2007, 08:10 PM
  2. Replies: 1
    Last Post: 04-07-2007, 08:11 AM
  3. Replies: 0
    Last Post: 09-23-2005, 02:17 AM
  4. How to solve unexpected font mapping
    By Rattagarn V. in forum VB Classic
    Replies: 0
    Last Post: 06-27-2000, 11:39 AM
  5. Solve this please
    By Ash in forum VB Classic
    Replies: 2
    Last Post: 05-18-2000, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center