Thread: Struct Values

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    33

    Struct Values

    I defined a struct in a header file.

    And now in a cpp file that includes that header, I want to access the struct's values.

    These variables were given values in another cpp file that includes the header file.

    But when I debug, the cpp that wants to access the struct's values sees them as nothing. The values are floats.

    So how do I get the values in a struct?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't quite understand. Do you want something like this:
    Code:
    //somestruct.h
    struct SomeStruct
    {
    SomeStruct(int val):a(val){}
    int a;
    
    }
    extern SomeStruct Obj;
    
    //source1.cpp
    #include "somestruct.h"
    SomeStruct Obj(10);
    
    //main.cpp
    #include <iostream>
    #include "somestruct.h"
    int main()
    {
      std::cout<<Obj.a;
    
    }
    static members might also work, but as I said, I don't really understand your question...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    can you just do :

    Code:
    //source.h
    struct astruct
    {
         int a;
    }mystruct;
    
    
    //source.cpp
    #include <iostream>
    #include "source.h"
    using namespace std;
    
    int main()
    {
         mystruct.a = 10;
         cout << mystruct.a << endl;
         cin.get();
         return 0;
    }

    this seem to work on VS.NET 2003

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is a difference between defining something and declaring an instance of something:

    Code:
    struct somestruct  // Defines a struct called "somestruct"
    {
        float x, y;
    };
    
    somestruct foo;  // Declares an instance of a somestruct struct
    If you have a multifile project (which it seems you do), you could not declare an instance of an object in the header file and then include the header file in all of the source files because the linker would complain about the same variable name existing in multiple places. So I will assume you have just defined the struct in the header (which is perfectly OK to include in multiple source files) and that the instance of the struct you are attempting to read the values from in one source file exists in another source file as in:

    header.h
    Code:
    struct foo
    {
        float x, y;
    };
    source1.cpp:
    Code:
    #include "header.h"
    
    foo myfoo;  // Declared an instance of struct foo called myfoo.

    Now, if you want to access the values of myfoo in this second source file you need to use the extern keyword as such.

    source2.cpp:
    Code:
    #include "header.h"
    
    extern foo myfoo;  // Says there is a foo struct called myfoo in another source
                       // file somwhere that we'd like to access the contents of in
                       //  this source file
    
    ...
    
    void somefunc()
    {
        float somefloatx = myfoo.x;  // Access myfoo's x member value
        float somefloaty = myfoo.y;  // Access myfoo's y member value
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    That does it, but brings up more problems.

    I'm using the struct not only for variables, but for DirectX frames.

    Now in the cpp where I create them, I create more than one of them, and with a different name for each. So I might have

    FrameStruct frPlayer;
    FrameStruct frEnemy;

    And what my function is doing is applying forces to all the frames.

    In the function I get the floating point that contains the weight of the frame. I get these from the struct.

    What I want is to get this function to go through all of them and apply the weight thats associated with each frame.

    I can get each frame and put it into the framestruct.frame value, but I cant get each framestruct's attributes (the variables).

    So, this might be confusing, but my problem is. If I extern the variables, I'll have to extern every single frame in my program, and do the function once for each frame. Which is not very efficient.

    Instead I want to use a for loop and apply the forces to all the frames.

    So to do that, I need a framestruct to be defined in the function so that it can just take the variables from the other framestructs and use them to change the frames' velocity.
    Last edited by Muphin; 08-13-2005 at 08:46 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Declaration/Definition

    I occasionally need to revisit this to keep clear on the distinction between a declaration and a definition:
    http://www.eskimo.com/~scs/C-faq/q1.7.html
    First, though there can be many declarations (and in many translation units) of a single ``global'' (strictly speaking, ``external'') variable or function, there must be exactly one definition. (The definition is the declaration that actually allocates space, and provides an initialization value, if any.)
    Of course, this is not quite the same as the declaration of a type (such as a forward reference) and the definition of a type.

    Mentioned FWIW.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM