loads of linker errors, and I only tried to clean up D:

This is a discussion on loads of linker errors, and I only tried to clean up D: within the C++ Programming forums, part of the General Programming Boards category; Hey again... I tried cleaning my code up abit, by adding some functions into other files (game.h and game.cpp) and ...

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    loads of linker errors, and I only tried to clean up D:

    Hey again...
    I tried cleaning my code up abit, by adding some functions into other files (game.h and game.cpp) and now I get

    Code:
    1>main.obj : error LNK2005: "class HGE * hge" (?hge@@3PAVHGE@@A) already defined in game.obj
    1>main.obj : error LNK2005: "class std::vector<class platform,class std::allocator<class platform> > & plats" (?plats@@3AAV?$vector@Vplatform@@V?$allocator@Vplatform@@@std@@@std@@A) already defined in game.obj
    1>main.obj : error LNK2005: "class worldManager mngr" (?mngr@@3VworldManager@@A) already defined in game.obj
    Now, I moved some functions from main.cpp, to game.h and game.cpp, and then I included game.h into main.cpp.
    I'm not specially good at VS2008, and some google site, talked about VS 6.0 or something, I couldn't figure that out tho...
    So is there something I can do with the compiler, or is it the code that can't be done like this?
    Thanks in advance
    Currently research OpenGL

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You put variable definitions into the header file. Bad.

    You can only put variable declarations into header files. So instead of
    Code:
    // game.h
    HGE* hge;
    you need
    Code:
    // game.h
    extern HGE* hge;
    
    // game.cpp
    HGE* hge;
    That's leaving aside the issue of having the global variable at all, of course.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It really depends. It looks like your game.h now contains globals, so each file that includes it will redefine them.

    If you really need them to be global (global reference to a vector?!), are you sure that you have declared them extern and then defined in exactly one source 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).

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Well, the engine tutorials use loads of globals, when it has something to do with the engine... 'Cause there are so many engine functions that need them xP so if I put them in global, every function knows of it, right?
    Currently research OpenGL

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    "already defined in game.obj"
    Means that a variable or function (with the same name) is defined two or more times in source files.
    How can this happen? The far most common error is to put non-inline functions and global variables inside headers. When they are included into each source file, you get duplicates, hence the error.

    Every function can access globals...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Yeah, and the most functions need access to HGE* hge xP I moved my other variables down to game.cpp, they're still globals there, but is this still foolish?
    Currently research OpenGL

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    There are good and bad uses of globals. You need to make sure they don't overcomplicate your design or create subtle bugs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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