A retrun card problem

This is a discussion on A retrun card problem within the C++ Programming forums, part of the General Programming Boards category; I am working on the solitaire game and I created an ADT called Rowstack. Inside the Rowstack, I have a ...

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

    A retrun card problem

    I am working on the solitaire game and I created an ADT called Rowstack. Inside the Rowstack, I have a function called
    "Card popFaceDownCard()" which returns the card at the top of the stack and if the stack is empty, the popCard should return a "RowStackEmpty" exception.

    My question is since the return type of this function is my another class
    "Card"

    so what can I do if i want to return the card?

    Since i tried to code the function like that:
    Code:
    Card rowstack::popFaceDownCard()
    {
             return card;
    }
    However, I get an error msg when i compiled it,
    so what should I do to fix this problem?
    thanks

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    1. Tell us what the errors you are getting are.
    2. Provide more context.

    Things to check:
    - That "card" is defined in the scope of the class, and is of type "Card".
    - Since this is a stack, you really need some sort of linked-list or array setup, so having a member of type "Card" seems unlikely.
    - As far as the exception goes, you do not return an exception, you throw an exception (and have stack unwinding), and the exception must be caught somewhere or your program will terminate.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Here is my rowstack.cpp
    Code:
    //rowstack.cpp
    #include <stack>
    #include <iostream>
    #include "rowstack.h"
    
    
    using namespace std;
    
    void Rowstack::pushFaceDownCard(const Card& card)
    {
    	rowstack.push_back(card); //Adds a card to the RowStack
    }
    
    
    
    Card::Card popFaceDownCard()
    {
    	return card;
    }
    and here is my rowstack.h
    Code:
    //rowstack.h
    #ifndef rowstack_h
    #define rowstack_h
    #include <stack>
    #include <cstdlib>
    #include "card.h"
    
    class Rowstack
    {
    public:
    	void pushFaceDownCard(const Card& card); //Adds a card to the RowStack
    
    
    	Card popFaceDownCard(); //Returns the card at the top of the stack 
    
    	
    	bool isFaceDownEmpty() const; //Returns true if the stack containing the face down card is empty
    
    
    
    private:
    
    	stack<Card> faceDownCards; // STL stack of Card ADT's
    
    
    
    };
    
    class RowstackException
    {
    public:
    		
    	RowstackException( RowstackExceptions newException);//sets the private attribute exception to new exception
    
    	RowstackException getException() const; //returns the private attribute exception
    
    
    
    
    private:
    
    	RowstackException exception;
    
    	
    
    };
    #endif
    Code:
    and this is my Card.cpp
    #include <cassert>
    #include <iostream>
    
    #include "card.h"
                        
    using namespace std;
    
    Card::Card() :
        rank( ACE ),
        suit( SPADES )
    {
    }
    
    Card::Card( Rank newRank, Suit newSuit ) :
        rank( newRank ),
        suit( newSuit )
    {
    }
    
    void Card::setRank( Rank newRank )
    {
        rank = newRank;
    }
    
    void Card::setSuit( Suit newSuit )
    {
        suit = newSuit;
    }
    
    ostream& operator<<( ostream& os, const Card& card )
    {
        switch( card.getRank() )
        {
            case Card::ACE:
                os << "Ace";
                break;
            case Card::TWO:
                os << "Two";
                break;
            case Card::THREE:
                os << "Three";
                break;
            case Card::FOUR:
                os << "Four";
                break;
            case Card::FIVE:
                os << "Five";
                break;
            case Card::SIX:
                os << "Six";
                break;
            case Card::SEVEN:
                os << "Seven";
                break;
            case Card::EIGHT:
                os << "Eight";
                break;
            case Card::NINE:
                os << "Nine";
                break;
            case Card::TEN:
                os << "Ten";
                break;
            case Card::JACK:
                os << "Jack";
                break;
            case Card::QUEEN:
                os << "Queen";
                break;
            case Card::KING:
                os << "King";
                break;
            default:
                assert( 0 );
        }
    
        os << " of ";
    
        switch( card.getSuit() )
        {
            case Card::SPADES:
                os << "Spades";
                break;
            case Card::HEARTS:
                os << "Hearts";
                break;
            case Card::CLUBS:
                os << "Clubs";
                break;
            case Card::DIAMONDS:
                os << "Diamonds";
                break;
            default:
                assert( 0 );
        }
    
        return os;
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I think I should use

    Code:
    Card::Card popFaceDownCard()
    {
            return card.top();
    }

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I just did a quick glance over your code, but one problem that stuck out:
    "card" is not defined in the scope of popFaceDownCard; the stack itself is called "faceDownCards"

    And yes, top() is the appropriate function for just retrieving the top element from the stack, though it will not pop that element off.

    Also, in your exception class. The class has a member which is of the same type. This is bad (and illegal -- just think of the infinite construction of objects this causes).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    So Are you mean that:
    Code:
    Card Rowstack::popFaceDownCard()
    {
            return popFaceDownCards.top();
    }
    I am not sure which variable should be coded after the return.
    But i think I should return the value which I declared in the private.
    Last edited by joenching; 05-07-2005 at 11:55 PM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I am not sure which variable should be coded after the return.
    But i think I should return the value which I declared in the private.
    Almost, because 'card' does not have scope in the function popFaceDownCard, but the actual stack, 'faceDownCards', is in scope since it is a class member.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You need to understand the scope of functions, i.e. what variables they can 'see'.

    A non-member function can see:

    1) the parameter variables and
    2) any variables declared inside the function

    For example:
    Code:
    void someFunc(int a, double b)
    {
    	int c = 10;
    
    	//other code
    }
    In that function, the parameter variables a and b are in scope, as well as the variable c because it is declared inside the function, so you can use their names inside the function:
    Code:
    void someFunc(int a, double b)
    {
    	int c = 10;
    
    	cout<<a + b + c<<endl;
    
    }
    A member function has a broader scope, and a member function can see:

    1) any parameter variables
    2) any variables declared inside it
    3) ALL the member variables of the class

    For instance,
    Code:
    class Card
    {
    
    private:
    	int x;
    	double y;
    
    public:
    	Card()
    	{
    		x=10;
    		y=25.5;
    	}
    
    	void aMemberFunc(int a, int b);
    	
    };
    
    void Card::aMemberFunc(int a, int b)
    {
    	int c = 2;
    	//some code
    }
    All the variables in red are in scope inside aMemberFunc(), and therefore you can use their names inside the function.

    However, no function can see the parameter variables of another function or the variables declared inside another function. So, when you post this:
    Code:
    Card rowstack::popFaceDownCard()
    {
             return card;
    }
    the only way the variable 'card' can be in scope--and therefore you are allowed to use that name inside the function--is if it is a member variable of the class. Since there is no parameter variable called 'card', and there is no variable declared inside the function called 'card', the only possibility left is if 'card' is a member variable(I'm ignoring the possibility that card is a 'global' variable).
    Last edited by 7stud; 05-08-2005 at 11:11 AM.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Thanks Sir.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rolodex(information manager) =>Check my Code
    By jackfraust in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2009, 04:41 AM
  2. New Graphics card! w00t!
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-15-2003, 05:38 AM
  3. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 11:00 AM
  4. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  5. Sign up: The Card Game Contest
    By ygfperson in forum Contests Board
    Replies: 40
    Last Post: 09-24-2002, 06:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21