Thread: struct access error

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    26

    struct access error

    Code:
    #include <curses.h>
    
    int s_palettep;
    int s_usflag;
    int s_cposr;
    int s_cposc;
    
    struct x_mttype   {   struct   {   unsigned int type    : 5;
                                       unsigned int obs     : 1;
                                       unsigned int mystery : 1;
                                       unsigned int locale  : 1;
                                       unsigned int link    : 1;
                                       unsigned int         : 7;
                                   } tile;
                          struct   {
                                       unsigned int tchar   : 8;
                                       unsigned int palette : 5;
                                       unsigned int bright  : 1;
                                       unsigned int         : 2;
                                   } tiledat;
                      };
    
    struct x_mttype s_maptemp[19][79];
    
    void clearmap();
    void edit();
    void ivar();
    
    int main()
    {
      ivar();
      initscr();
      keypad ( stdscr, TRUE );
    
      edit();
    
      endwin();
      return 0;
    }
    That is main.cpp

    Code:
    extern struct x_mttype s_maptemp;
    
    void clearmap()
    {
      for (int a = 0; a < 20; ++a)
      {
        for (int b = 0; b < 80; ++b)
        {
           s_maptemp[a][b].tile.type=0;
        }
      }
    }
    That is clearmap.cpp

    My problem is that I get this error, when I compile and run the program:

    Code:
    9 clearmap.cpp
     no match for `x_mttype &[int]'
    Last edited by sufthingol; 03-22-2005 at 05:57 PM.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Not certain since I'm not too familiar with the extern command, but could it be because you declare your map as an array in main but only as a single instance in clearmap.cpp?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    i still get errors when i add the brackets

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Heres an idea instead of having a global why not pass it to the function?
    Woop?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    You can't really do that in roguelikes. Would not I have to send a pointer to the object, to the function, though?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    Explain how passing works, if thou dost please.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    just a guess

    extern struct x_mttype s_maptemp[][];
    or
    extern struct x_mttype s_maptemp[][79];
    or
    extern struct x_mttype s_maptemp[19][79];
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You would pass the struct as you would any other array.
    Code:
    #include <iostream>
    using namespace std;
    
    struct map
    {    
        int x;
        int y;
    };
    
    void setXY(map setMap[],const int size)
    {
        int i = 0;
        while(i < size)
        {
            setMap[i].x = 3;
            setMap[i].y = 6;
            i++;
        }
    }
    
    int main()
    {
        int i = 0;
        map mainMap[5];
        setXY(mainMap,5);
        while(i < 5)
        {
            cout<<mainMap[i].x<<endl;
            cout<<mainMap[i].y<<endl;
            i++;
        }
        cin.get();
        return 0;
    }
    Woop?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    Look, I don't think you can access a variable in main from a function called in main; you're gonna have to explain that to me, it's just not makin' any sense.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    struct x_mttype   {   struct   {
    Put this in a header file, and include it in all the source files which refer to it.

    > extern struct x_mttype s_maptemp;
    Specify some dimensions - like the ones you used when you declared it.

    > for (int a = 0; a < 20; ++a)
    Should be < 19, since that's all you specified.
    You're stepping off the ends big-time here.
    A better idea would be to have some constants for the dimensions, rather than littering the code with all these magic numbers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Look, I don't think you can access a variable in main from a function called in main;
    Of course not. But you can access a pointer or reference to the variable, if it's passed as a parameter (argument) to the function. The reverse is not true (main cannot access the variables in a function it calls) because any variables declared within the function, with the exception of static variables, will go out of scope when the function ends.

    Functions:
    http://www.cprogramming.com/tutorial/lesson4.html

    Pointers:
    http://www.cprogramming.com/tutorial/lesson6.html
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM