Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Passing structs in functions

  • 03-04-2007 12:27pm
    #1
    Closed Accounts Posts: 408 ✭✭


    Hey,

    quick question:

    When you are passing a struct to a function how exactly is the best way (in c++) to call that function ?

    For example:

    unit::renderSelection(SDL_Surface *screen, unitlocs location) // << unitlocs is a //struct + location is an object of it

    But when i go to call the function later in main like so:
    (inside a draw() method)
    theUnit->renderSelection(screen, location);

    I get errors that location is undeclared.

    I have included the class that the function comes from in my main, and my pointer is as follows:

    unit *theUnit = new unit();

    So can anyone enlighten me as to how to properly call this function ? I tried using a pointer to the struct but that didnt work either :(


Comments

  • Registered Users, Registered Users 2 Posts: 1,287 ✭✭✭joe_chicken


    Have you tried declaring an instance of unitlocs and passing that in?

    Not sure of the interface you're using, but I presume an instance of SDL_Surface* called "screen" is declared somewhere...

    structs get passed in the same way as everything else


  • Closed Accounts Posts: 408 ✭✭Chada


    Sorry i wasn't clear before, SDL_Surface *screen is a pointer to the surface where the renderSelection is going to be drawn to, its fine.

    Its this struct unitlocs, i've created instances of it like so:
    struct unitlocs {
    Point2D location;
    Point2D destination;
    }
    
    

    With point2d being another struct in a different class with generic getX() and getY() methods.


    When i call the unitlocs location into a function, im able to make struct method calls like location.getX() etc. without a problem.

    But when i try and call the actual function renderSelection() from main, it doesn't recognise location as an instance of unitlocs.

    As iv shown before iv tried many variances but still get errors:
    
    theUnit->renderSelection(screen, location);
    
    theUnit->renderSelection(screen, unitlocs location);
    
    

    How do you call a method with an external struct in it without get these undeclared errors ?


  • Registered Users, Registered Users 2 Posts: 1,287 ✭✭✭joe_chicken


    try use typedef struct
    typedef struct { 
    
    Point2D location;
    Point2D destination;
    
    } unitlocs;
    
    unitlocs unitlocs_struct;
    theUnit->renderSelection(screen, unitlocs_struct);
    


  • Closed Accounts Posts: 408 ✭✭Chada


    Tried your way mate, got the same error:

    128 C:\DanZelda\DanZelda\main.cpp `unitlocs_struct' undeclared (first use this function)

    Iv'e included all the files needed im just kinda stuck as to why it won't let me call the function.

    this is the draw() function in main im trying to get it to run in:
    void draw(engine *theEngine, SDL_Surface *back, SDL_Surface *screen, Selection *theSelection,
     unit *theUnit, player *thePlayer, vector<orc *> badGuys, vector<sprite *> goodGuys, vector<unit *> units, vector<baseBuilding *> buildings, vector<sprite *> screenInfo)
    {
        theEngine->Display->drawSurface(back, backX, backY, screenWidth, screenHeight, screen);  
        drawSprite(thePlayer, theEngine, screen); 
    
        vector<sprite *>& playerArrows = thePlayer->getWeapon();
        for(unsigned int i=0; i < playerArrows.size(); i++)
        {
            if(playerArrows[i] != NULL)
            {
                drawSprite(playerArrows[i], theEngine, screen);
            }
        }
    
    
        for(unsigned int i=0; i < goodGuys.size(); i++)
        {
            drawSprite(goodGuys[i], theEngine, screen);
        }
        
        for(unsigned int i=0; i < badGuys.size(); i++)
        {
            drawSprite(badGuys[i], theEngine, screen);
        }
        for(unsigned int i=0; i < units.size(); i++)
        {
            drawSprite(units[i], theEngine, screen);
        }
        for(unsigned int i=0; i < buildings.size(); i++)
        {
            drawSprite(buildings[i], theEngine, screen);
        }
    
        
        for(unsigned int i=0; i< screenInfo.size(); i++)
        {
            drawSprite(screenInfo[i], theEngine, screen);
        }
    
        vector<sprite *>& playersLife = thePlayer->getLifeIcon();
        for(unsigned int i=0; i < thePlayer->getLife(); i++)
        {
            if(playersLife[i] != NULL)
            {
                drawSprite(playersLife[i], theEngine, screen);
            }
        }
    
        string msg = font->IntToString(thePlayer->getScore());
        int fw = (font->getTextWidth(msg))/2;
        font->write(screen,msg,140-fw,20);
        
        
        //drawMySprite(goodGuys[0], theEngine, screen);
        theSelection->update();
        theSelection->render(screen);
        [B]theUnit->unitSelection(selection, unitlocs_struct);
        theUnit->renderSelection(screen, unitlocs_struct);    [/B]                 
        SDL_Flip(screen);
        
    
    }
    

    Anything look wrong there ? All the pointers called by draw are delcared inside main.


  • Registered Users, Registered Users 2 Posts: 1,287 ✭✭✭joe_chicken


    where are you creating the instance of unitloc?

    you should do that from inside the function, and make sure to initialise...

    i.e.
    typedef struct {
           ......
    } mystruct;
    
    
    function()
    {
         mystruct a_struct;
         
    ...........
    ...initialise......
    ...........
    
    
         use_struct(a_struct);
    }
    


  • Advertisement
  • Closed Accounts Posts: 408 ✭✭Chada


    Ah right im with ya now, so just so i duplicate this technique over to my functions and to make sure i'm right:
    typedef struct unitlocs{
    Point2D location;
    Point2D destination;
    
    int getXunitloc(blah)
    {
    ......
    }
    int getYunitloc(blah)
    {
    .....
    }
    } unitLoc;
    
    class unit: public sprite
    {
    renderSelection(SDL_Surface *screen, unitLoc unitlocs_struct)
    }
    // now inside me .cpp file:
    
    ....
    
    void unit::renderSelection(SDL_Surface *screen, unitLoc unitlocs_struct)
    {
        unitLoc unitlocs_struct = getUnitlocation();    
    
         if(unitState == SELECTED)
         {
           circleColor( screen, unitlocs_struct.getXUnitLocation(), unitlocs_struct.getYUnitLocation(), 10 , 0xfe00008b);         
         }            
         
    }   
    
    }
    

    Is that correct ?

    And then call theUnit->renderSekection(screen, unitlocs_struct) in main.cpp ?


  • Registered Users, Registered Users 2 Posts: 1,287 ✭✭✭joe_chicken


    if you want to put functions in your struct, you should use a class


  • Closed Accounts Posts: 408 ✭✭Chada


    Thanks for help joe, got it working by delcaring an instance of each struct in my header file and then it was able to look them up from there.

    Although my renderSelection still isn't drawing the circle around the unit but thats another story :)


  • Closed Accounts Posts: 408 ✭✭Chada


    Thanks for help joe, got it working by delcaring an instance of each struct in my header file and then it was able to look them up from there.

    Although my renderSelection still isn't drawing the circle around the unit but thats another story :)


Advertisement