Thread: Multiple definitions?

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Multiple definitions?

    In a program I'm working on, I have a header file which defines some structs. In that same header file I have 2 pointers:
    Code:
    Window* gwindow; // global pointer to core Window obj
    Settings* gsettings; // global pointer to core Settings obj
    I want to use these pointers globally throughout the program, so that wherever I include the header file, I have access to them. However, while linking I get these errors:

    multiple definition of `gwindow'
    first defined here
    multiple definition of `gsettings'
    first defined here

    It must say that about 5 times each for gwindow and gsettings. What does this mean? I'm guessing everytime I include the file, it redefines the the pointer or something? How do I get around this?
    Using Dev-C++ on Windows

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sent from my iPadŽ

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Of course, you could pass the pointers around in parameters. Maybe put the pointers in the main .cpp file, and in the other .cpp files where you need them, use the 'extern' keyword (meaning you're expecting to find that pointer from a different file). I believe its like Settings* gsettings; in main.cpp, and extern Settings* gsettings; in settings.cpp. I haven't done that in a while (and I only did it once) so I'm probably wrong somehow.

    If the program design is laid out right would a global, or extern, really be necessary? I can't tell, but I don't think passing references would be able to reach everything in a program (especially main()). Any other ways?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Hmm.. so would I put:
    Code:
    extern Window* gwindow;
    extern Settings* gsettings;
    at the beginning of each file I include the header? It still does the same thing.
    Using Dev-C++ on Windows

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No the other way round.
    You put
    Code:
    extern Window* gwindow;
    extern Settings* gsettings;
    In the header thet gets included in severel source-files.
    and define the variables in any ( one ) of your source-files.
    Kurt

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Heres some modified code for a singleton that I got out of a book. This is sort of what I was thinking of doing, but thought it would be cheesy, but this looks good. It creates an instance of Window and returns a pointer to that instance, unless one has already been created in which case it returns that instance. Hence only one instance of the class exists in the program. Both extern and this would get the job done, just passing this by. (since I do find it more elegant).

    Code:
    #window.h
    
    class Window {
    public:
       static Window* Instance();
    protected:
       Window() {}
    private:
       static Window* m_instance;
    };
    
    Window* Window::m_instance = 0;
    
    Window* Window::Instance () {
       if(m_instance == 0) {
          m_instance = new Window;
       }
    
       return m_instance;
    }
    It doesn't say, but I would think calling: Window* gwindow = Window::Instance(); in the files you need gwindow would work, or call it directly and skipping the gwindow part.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. multiple definitions of msg
    By laasunde in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2002, 09:31 PM