one header file defines a struct and the other uses it.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: one header file defines a struct and the other uses it.

  1. #1
    Join Date
    Dec 2010
    Posts
    583

    one header file defines a struct and the other uses it.

    Code:
    // mesh.h
    struct D3DXMESHCONTAINER_DERIVED
    {
    };
    
    
    //  allocmeshhierarchy.h
    #include "mesh.h"
    void GenerateSkinnedMesh (LPDIRECT3DDEVICE9 pDevice, D3DXMESHCONTAINER_DERIVED* pMeshContainer);

    [translated]
    error C2011: D3DXFRAME_DERIVED repeated definition of d: \ users \ documents \ visual studio 2010 \ projects \ perfectsim v5.0 \ perfectsim v5.0 \ mesh \ mesh.h the 11 1 PerfectSim V5.0
    What is correct linkage method?
    Thanks
    Jack
    Last edited by lucky6969b; September 19th, 2012 at 02:16 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,271

    Re: one header file defines a struct and the other uses it.

    Quote Originally Posted by lucky6969b View Post
    error C2011: D3DXFRAME_DERIVED repeated definition of d: \ users \ documents \ visual studio 2010 \ projects \ perfectsim v5.0 \ perfectsim v5.0 \ mesh \ mesh.h the 11 1 PerfectSim V5.0
    What is correct linkage method?
    Thanks
    Jack
    What does linkage have to do with this compiler error C2011?
    And, BTW, I don't see any D3DXFRAME_DERIVED type in the code snippets you have posted.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,422

    Re: one header file defines a struct and the other uses it.

    What is correct linkage method?
    Where are your include guards for that header? That is a compiler error, and the reason is more than likely that you do not have include guards to prevent the definition being encountered two (or more times) per compilation unit.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2010
    Posts
    583

    Re: one header file defines a struct and the other uses it.

    Sorry, I have solved it myself

    First
    AllocMeshHierarchy.h
    #include "mesh.h"

    Second
    AllocMeshHierarchy.cpp
    #include "AllocMeshHierarchy.h"

    third
    mesh.h
    struct D3DXMESHCONTAINER_DERIVED //etc
    Thanks both of you
    Jack
    Last edited by lucky6969b; September 19th, 2012 at 03:24 AM.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: one header file defines a struct and the other uses it.

    You don't have include guards in your header, which means each symbol defined there will be created once in each translation unit (cpp file) that includes the header. You need either this:
    Code:
    #pragma once
    
    struct D3DXMESHCONTAINER_DERIVED
    {
    };
    or
    Code:
    #ifndef _THEHEADER_
    #define _THEHEADER_
    
    struct D3DXMESHCONTAINER_DERIVED
    {
    };
    
    #endif // end of header
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,422

    Re: one header file defines a struct and the other uses it.

    Quote Originally Posted by lucky6969b View Post
    Sorry, I have solved it myself
    That is no solution. All you're doing is delaying the inevitable, and that is two or more inclusions of the header file in a single compilation unit.

    The actual solution is to use include guards, as cilu and myself have pointed out. With a more complex program, you would go crazy trying to figure out what header goes first, second, third, etc. without include guards. You may not even have a scenario where you can guarantee that the header is seen only once by the compiler, so how are you going to solve that issue without using include guards?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center