Thread: Getting multiple Definitions Error ( C++)

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    Getting multiple Definitions Error ( C++)

    EzGame.cppEzGame.hTetris.hTetris.cpptgame.cpp

    I keep getting Multiple definition errors with the function members from EzGames... I used this file before to make a simple game, and I'm trying to figure out why I keep getting multiple definition errors....
    Maybe it's in my tetris.cpp file..? I'm not really sure... Any advice or heads up

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You honestly think people are going to search through a bunch of files for your problem? One trick of asking questions, at least if you want useful answers, is to do a bit of groundwork rather than expecting someone else to do it all for you.

    Basically, you need to look through ALL your files looking for where the offending members are defined (implemented). If they are defined in more than one place (a simple search will detect that) then that's your problem.

    If they are only defined in one place, then it gets a little trickier.

    1) If that place is in a header file, they need to be inline. Otherwise, if that header is #include'd by multiple source files, that would explain your problem.
    2) If that place is in a source file (.cpp) then your problem might be due to that source file being #include'd by other files.
    3) If neither of the above is true, then check how the linking is being done. You need to ensure that no object files are specified more than once in the link.

    There are a couple of additional complexities and exceptions if the offending functions are templates but the basic guidelines above still apply.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Actually, you are right. Instead, I used an anonymous namespace for one part of the code...
    Last edited by Darkinyuasha1; 06-26-2012 at 11:31 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    actually, no it didn't... it's weird.. I isolated the EzGame header /cpp and even with namespace it gives me multiple definition for each function.EzGame.cppEzGame.h

    I even tried with the inline for the function members in each class... but then the multiple definition erros turn into undefine function ?!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Using an anonymous namespace would not change a problem with multiply defined symbols.

    The problem - as I said in my previous post - is in how your source is organised. If the compiler sees a function definition when compiling distinct source files, it will output that definition (in compiled form) to multiple object files. The linker will see that definition in multiple distinct object files, hence will complain about multiple definitions.

    I've had a quick look at all of your files, and you are making extensive usage of non-standard headers, which will prevent most people from recreating your problem even if they were willing to. Your code is also large enough that most will be unwilling to dredge through it looking for the cause of your problem. There are limits to the help you can get from unpaid volunteers, after all.

    What you need to do (after backing up your project so you can restore it) is remove elements of code, and try to create small but complete samples of code (probably in multiple files, as the compiler would also complain if it encountered multiple definitions when compiling a particular source file) that exhibit your problem. My previous post mentioned the types of things you need to look for. In the process of creating that small but complete sample, you might have an "aha!" moment. If not, you will have a clear and more concise sample that other folks here will be willing/able to help you with.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 03-07-2011, 05:28 PM
  2. Functions with multiple definitions
    By Jdo300 in forum C Programming
    Replies: 4
    Last Post: 03-13-2010, 06:55 PM
  3. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  4. Multiple definitions?
    By Ganoosh in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2005, 05:20 AM
  5. multiple definitions of msg
    By laasunde in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2002, 09:31 PM