Thread: Multiple definition error, one definition

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    31

    Question Multiple definition error, one definition

    I am trying to finish off a text RPG, and a strange problem has cropped up. I get the error "multiple definition of [x]", and then a reference to where it was 'first' defined, being a function that only uses the objects, but does not define them. Here is a rundown of the set up:

    In header ENTITIES_H, an array of items is created like this. The intention is for them to be global. ENTITIES_H uses #ifndef. Note that the compiler does not say the objects were first defined here. What are accused of being redefined are potion, ether, etc..:

    Code:
    Item items[5] = { potion, ether, bandage, eyeDrops, antigen };
    These hold the characteristics of the items, and are only instantiated once. The inventory is an integer array that holds the number of each item held.

    This header is included in two files, SHOP.CPP and PLAYER.CPP, which both make references like:
    Code:
    for (int i=0;i<5;i++) //Determine if the player has anything
        {
            int check = 0;
            check += inventory[i];
            if ( inventory[i] > 0 )
                cout << i+1 << ". " << items[i].itemName << " - x" << inventory[i] << endl;
        }
    Why is the compiler telling me I am attempting to redefine them? There is absolutely no instantiation of class Item outside of ENTITIES_H.

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You probably need header guards in your header.

    Code:
    #ifndef ENTITIES_H
    #define ENTITIES_H
    
    //////////////  all other entries go here
    
    
    #endif

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Quote Originally Posted by jimblumberg View Post
    You probably need header guards in your header.
    They are already included.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by frog View Post
    Why is the compiler telling me I am attempting to redefine them? There is absolutely no instantiation of class Item outside of ENTITIES_H.
    Instantiating an object inside a header file is the cause of the problem.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is not the compiler complaining about multiple definitions. It will be the linker.

    And the reason, as brewbuck noted, is that your a defining the variable in a header file - which means it is defined in every source file that #include's that header. You are #include'ing that header in two source files, so your array of items is defined twice.
    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.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    I get the deal about multiple definitions now, even though at first it wasn't apparent to me. I included extern declarations in the header file, and defined them in a source file.

    It now links just fine. However, I have another problem: I could only get this to work by defining the Items in the global area in main.cpp. I had first tried to define them in an items.cpp file, but they were not recognizedas being there. While effectively this setup is the same, it is butt-ugly. Why is it not recognizing the definitions in items.cpp?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Without seeing your code, how can we know?

    Jim

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    When building the executable, the main.o and items.o need to BOTH be supplied to the linker (as parameters on the command line).
    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.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    try #pragma once

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Eman View Post
    try #pragma once
    .... if you wish to have a non-portable approach that also doesn't address the problem being discussed in this thread.
    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. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM