GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jul-2006, 16:57
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

passing locally defined user type to function from function


If I prototype and call a function from within another function, passing a locally defined user type as a variable, as in:

CPP / C++ / C Code:
//
void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[])
{
    int card;
    
    //locally defined user type hand
    struct hand {
        int face[6];    //only elements 2 - 6 are used
        int suit[6];
    };
    hand pokerHand;
    
    //
    //code removed that actually dealt the hand into pokerHand
    //
    void onePair (const hand);  // function prototype
    onePair (pokerHand);        //call function using local variable pokerHand
}

//function definition - not yet written (obviously)
//called only from function deal
void onePair (const hand newDeal)
{

}

I get the following errors:

Quote:
main.cpp:74: error: non-local function 'void onePair(deal(const int (*)[13], const char**, const char**)::hand)' uses local type 'deal(const int (*)[13], const char**, const char**)::hand'
main.cpp:79: error: ISO C++ forbids declaration of 'hand' with no type
main.cpp:79: error: expected ',' or '...' before 'newDeal'

(note that the line numbers above don't correspond to the code posted)

How do I get this to work? Do I have to use the scope resolution operator somehow? Do I have to define struct hand somewhere other than within the local function? I know I can define hand as a global variable but should I or is there another way?
  #2  
Old 01-Jul-2006, 17:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,520
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: passing locally defined user type to function from function


Quote:
Originally Posted by earachefl
How do I get this to work?


The definition for the struct must be visible wherever it is used. By declaring it inside of function deal(), it isn't known anywhere else.

You could do something like this
CPP / C++ / C Code:
// this type of structure will be known everywhere after this in the current file

struct hand {
    int face[6];    //only elements 2 - 6 are used
    int suit[6];
};

void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[])
{
    int card;
    
    hand pokerHand;
    
    //
    //code removed that actually dealt the hand into pokerHand
    //
    void onePair (const hand);  // function prototype
    onePair (pokerHand);        //call function using local variable pokerHand
}

//function definition - not yet written (obviously)
//called only from function deal
void onePair (const hand newDeal)
{

}


Note that the instance pokerHand is still local to its function (it's not a global or file scope variable). Struct definitions and other typedef statements are typically placed in headers, but they don't have to be.


Regards,

Dave
 

Recent GIDBlogUpdates On The All New Toyota VIOS - Part III by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Program goldfish C Programming Language 6 13-May-2006 13:12
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.