Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio
  4. Simple c++ ques: how to define a struct over mult files?

Simple c++ ques: how to define a struct over mult files?

Scheduled Pinned Locked Moved Visual Studio
c++tutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    largeinsd
    wrote on last edited by
    #1

    Hi, Just a basic question: How do you define a struct so that it's visible over multiple files? For example, for a function I know you would create a header file and include the forward declaration, there... while implementing the function in a .cpp file. Is there a similar forward declaration for a struct, or do I have to create a namespace ? Thanks - @LG

    J 1 Reply Last reply
    0
    • L largeinsd

      Hi, Just a basic question: How do you define a struct so that it's visible over multiple files? For example, for a function I know you would create a header file and include the forward declaration, there... while implementing the function in a .cpp file. Is there a similar forward declaration for a struct, or do I have to create a namespace ? Thanks - @LG

      J Offline
      J Offline
      JudyL_MD
      wrote on last edited by
      #2

      I think this is as straight-forward as it appears ... define the structure in a header file and include the .h in any source file that needs to reference the type Judy

      L 1 Reply Last reply
      0
      • J JudyL_MD

        I think this is as straight-forward as it appears ... define the structure in a header file and include the .h in any source file that needs to reference the type Judy

        L Offline
        L Offline
        largeinsd
        wrote on last edited by
        #3

        Hi Judy, Thanks for your reply. The trouble I was having with that approach is that the compiler would give me an error stating that I was multiply defining the struct in question. So, poking around on the net a bit I found this great page on the preprocessor and "header file etiquette"... http://www.codeguru.com/cpp/tic/tic0056.shtml and it explained how to use an "include guard" (#ifdef) to protect from the problem that I was having. -@LG

        J 1 Reply Last reply
        0
        • L largeinsd

          Hi Judy, Thanks for your reply. The trouble I was having with that approach is that the compiler would give me an error stating that I was multiply defining the struct in question. So, poking around on the net a bit I found this great page on the preprocessor and "header file etiquette"... http://www.codeguru.com/cpp/tic/tic0056.shtml and it explained how to use an "include guard" (#ifdef) to protect from the problem that I was having. -@LG

          J Offline
          J Offline
          JudyL_MD
          wrote on last edited by
          #4

          When using the microsoft compilers, you can also do that by using the line #pragma once at the top of your header file. glad you found it - I never write a header file without something to force single inclusion Judy

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups