Thread: Structure help

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Structure help

    Why doesn't my structure work, I did the same thing as the tutorial on this site!

    struct ship
    {
    int x_pos;
    int y_pos;
    };

    ship player;
    player.x_pos = 256;
    player.y_pos = 256;

  2. #2
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hi,

    I cant see anything wrong with that code all i can think is that you have it in the wrong place: here is a complete working example:


    Code:
    #include <iostream.h>
    
    struct ship 
    { 
    int x_pos; 
    int y_pos; 
    }; 
    
    
    int main()
    {
    
    ship player; 
    player.x_pos = 256; 
    player.y_pos = 256;
    
    
    return 0;
    }
    That compiles fine in Visual C++ 6.

    Hope that helps

    TNT

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    No, that isn't it.
    Im defining this in a header file, does that make any differnece??
    Last edited by Unini; 09-16-2001 at 06:38 AM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    I couldn add variables inside a header file.
    I moved it outside the header file into a cpp file and now it works.
    I should have looked a t my sig more

  5. #5
    Unregistered
    Guest
    > player.x_pos = 256;
    This line has to be inside a function - there is no problem with the rest of your original post being in a .h file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM