Thread: Redefinition error with repititions of header file declaration

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    193

    Wink Redefinition error with repititions of header file declaration

    Hi,

    I'm not sure if this is a compiler problem. I have the following in one file:

    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    #include "MeshFiles.h"
    And this in another:
    Code:
    #include <d3d9.h>
    #include <d3dx9.h>
    #include "MeshFiles.h"
    And I get a redeclaration error. A new file opens up and in it is nothing but a blank screen amd a cursor. It's a directX9 file too. Now I understand it is likely being caused by the same header included in two files, but how on earth can I give each file independent access to the same code when they both need it without causing this error?

    Is this a compiler issue? I'm using Code::Blocks has the GNC compiler I think. Anyways, I'm stumped!! Any ideas - can I just lay my code out in a different way to get round this? Thanks

    ***Edit***

    here's the specific error:

    Code:
    obj\Debug\Initialise.o:C:\Program Files\Microsoft 
    DirectX SDK (February 2010)\Include\d3d9types.h|51|
    multiple definition of `MeshSoldier::load_soldier(IDirect3DDevice9*)'|
    Last edited by shrink_tubing; 08-01-2010 at 12:06 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    193
    Even more bizarre - when I converted the offending file from a .cpp to a .h file it ran. Seems I don't understand how to properly implement multiple .cpp files in one project. Can anyone help me out here?

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You would need to provide more information about your code. Especially about your MeshFiles.h. I don't understand why you changed extension.

    Seems I don't understand how to properly implement multiple .cpp files in one project.
    You probably include them all in MeshFiles.h, don't you?
    Never include ".cpp" files. Click your project from file tree and choose "Add files..." and add all the .cpp files.
    Also, ensure MeshFiles.h (and all your headers) has the:
    #ifndef MESHFILES_H
    #define MESHFILES_H
    ...
    #endif
    or
    #pragma once
    at the beginning

    Also you should read about precompiled headers.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    193

    Thumbs up

    Thankyou sir/madam. In the end it was a combination of EXACTLY what you just said and also me having the same class declaration in two header files.

    My header file meshFiles.h which holds the class def MeshSolider appears in two places. i.e. #include meshFiles.h appears twice. Once for one .cpp file and once for another.

    Not really a problem you might think, and it isn't - provided your function calls are defined INSIDE the class! Only when I defined my func calls outside the class did I get a redeclaration error.

    All sorted out now, guess I need to move past this idea of defining member functions outside of classes eh?

    Thanks alot mate.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No. What you need to do is put the class definition in a header. Make sure to use include guards as as outlined above.
    Then you put the implementation in a .cpp file. Not in the header file.'
    Then it will work.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    193
    Sure thing mate, thanks to you guys - it ran

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM