Thread: How do I access elements of a struct from a function where the element isn't declared

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

    How do I access elements of a struct from a function where the element isn't declared

    In one of my functions, I've declared a structure.

    I want to be able to modify the contents of that structure (specifically an array I've defined) from another function, but I keep getting an error saying my structure is undeclared.

    How do I go about doing this?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    When in doubt post code.
    Woop?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Alright, here goes (I've excluded my main() even though it's very short):

    Also, I'm able to compile my program now that I've moved my structure out from gameLoop to the top of my code as a global variable, the only problem is I don't get any images loading anymore which I assume has to do with the fact that I'm destroying the data at the end of my loadTiles() function, before the drawTiles() function.

    This seems wrong to me, but should I place both my loadTiles() and drawTiles() functions INSIDE my gameLoop() function? I've always liked keeping everything separate, but I've never made a game before...

    Code:
    //create the world map
    struct MAP {
        BITMAP *map[40][30];
        int  tileID[40][30];
    };
        
    MAP worldMap; 
    
    void loadTiles(int worldX, int worldY){
        
        //load the tiles from tiles.dat 
        DATAFILE *tiles = load_datafile("tiles.dat");
        
        //load the tiles into memory
        BITMAP *mapTile_grass     =  (BITMAP*)tiles[Grass].dat;
        BITMAP *mapTile_mountain  =  (BITMAP*)tiles[Mountain].dat;
        BITMAP *mapTile_town      =  (BITMAP*)tiles[Town].dat;
    
        //load world map from worldmap.txt
        ifstream mapInput("worldmaplarge000.txt");
        
        for(int y=0; y<worldY; y++){
            for(int x=0; x<worldX; x++){
                mapInput >> worldMap.tileID[x][y];
            }
        }   
            
        mapInput.close();
    
        //load tiles to the proper coordinates
        for(int y=0; y<worldY; y++){
            for(int x=0; x<worldX; x++){
                if (worldMap.tileID[x][y] == 0){
                    worldMap.map[x][y] = mapTile_grass;
                }
                else if (worldMap.tileID[x][y] == 1){
                    worldMap.map[x][y] = mapTile_mountain;
                }
                else if (worldMap.tileID[x][y] == 2){
                    worldMap.map[x][y] = mapTile_town;
                }
            }
        }
        
        //unload data before exit
        destroy_bitmap(mapTile_grass);
        destroy_bitmap(mapTile_mountain);
        destroy_bitmap(mapTile_town);
        unload_datafile(tiles);    
        
        return;
    }
    
    void drawTiles(int screenX, int screenY){
        
        //draw the map into the buffer
        BITMAP *buffer = create_bitmap(320, 240); 
    
        //draw tiles to the screen
        acquire_screen();
        
        for(int y=0; y<screenY; y++)
        {
            for(int x=0; x<screenX; x++)
            {
                draw_sprite(buffer, worldMap.map[x][y], x*16, y*16);
                blit(buffer, screen, 0, 0, 0, 0, 320, 240);
            }
        }
        
        release_screen();
        
        //unload data before exit
        destroy_bitmap(buffer);
        
        return;    
    }
    
    void gameLoop(){
        
        //map coordinates
        int worldX=40;
        int worldY=30;
        int screenX=20;
        int screenY=15;
        
        //load the tiles into memory
        loadTiles(worldX, worldY);
        
        //drawTiles to the screen
        drawTiles(screenX, screenY);
        
        //quit on keypress
        readkey();     
        
        return;
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Local variables that are declared inside a function are destroyed when the function finishes executing. However, if you dynamically allocate memory for a new object using the new operator, you can return a pointer to that object. The pointer variable you declare inside the function will inevitably be destroyed since it is a local variable, but the address it contains will be copied and returned to the calling function--giving the calling function the location of the new object, and the object will not be destroyed until you manually delete it yourself. So, you can do something like this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct A
    {
    	A(int num, string text){ n=num; str = text;}
    	int n;
    	string str;
    };
    
    A* doSomething(void)
    {
    	//declare a pointer to an A struct, and dynamically allocate it memory:
    	A* p = new A(10, "hello"); 
    	
    	return p;
    }
    
    void doSomethingElse(A* ptr)
    {
    	cout<<ptr->n<<endl;
    	cout<<ptr->str<<endl;
    }
    
    int main ()
    {
    	A* temp = doSomething();
    	doSomethingElse(temp);
    
    	delete temp; //delete the memory allocated with new
    
    	return 0;
    }
    Last edited by 7stud; 03-22-2005 at 03:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM