Thread: A TestUnit file cannot be compiled..

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

    A TestUnit file cannot be compiled..

    Hi all, when i tried to compile those files, my compiler told me that I have 3 errors
    those errors are:
    Compiling...
    rowstackTest.cpp
    c:\lab8\rowstacktest.cpp(17) : error C2065: 'faceDownCards' : undeclared identifier
    c:\lab8\rowstacktest.cpp(17) : error C2228: left of '.pushFaceDownCard' must have class/struct/union type
    c:\lab8\rowstacktest.cpp(17) : error C2275: 'Deck' : illegal use of this type as an expression
    c:\lab8\deck.h(8) : see declaration of 'Deck'

    Here is my RowstackTest.cpp
    Code:
    #include <iostream>
    #include <stack>
    #include "rowstack.h"
    #include "deck.h"
    #include "card.h"
    
    using namespace std;
    
    int main()
    {
    	Deck MyDeck;     // Instantiate a Deck object;
    
    	Rowstack MyRowstack;  // Instantiate a Rowstack object;
    	
    	for(int iCtr = 0; iCtr < 6; iCtr++)
    	{
    		faceDownCards.pushFaceDownCard(Deck[iCtr]);
    	}
    
    	return 0;
    }
    Here is my rowstack.cpp
    Code:
    //rowstack.cpp
    #inclued <cstdlib>
    #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 RowstackL::popFaceDownCard()
    {
    	if ( empty() )
    	{
    		throw RowStackExceptions::RowstackEmpty;
    	}
    	 return popFaceDownCards.top(); // returns the card at the top of the stack.
    }
    here is my rowstack.h
    Code:
    //rowstack.h
    #ifndef rowstack_h
    #define rowstack_h
    #include <stack>
    #include <cstdlib>
    #include "card.h"
    #include "deck.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:
    		
    	enum RowstackExceptions
    	{
    		RowstackEmpty
    	};
    	
    	RowstackException( RowstackExceptions newException ) :
            exception( newException ) {}      //sets the private attribute exception to new exception
    
    	RowstackExceptions getException() const {return exception;} //returns the private attribute exception
    	
    private:
    
    	RowstackExceptions exception;
    
    	
    
    };
    #endif
    and that one is my deck.h
    Code:
    #ifndef deck_h
    #define deck_h
    #include <vector>
    #include <cstdlib>
    #include "card.h"
    
    class Deck
    {
    public:
    	enum {NUM_CARDS = 52};
    
    	Deck(); // default constructor, sets the initial size of 52
    
    	void shuffle(); // method of shuffle cards
    
    	Card& operator[] (int); // Overload the [] operator. 
    
    
    private:
    	vector<Card> card; // STL vector of type card.
    
    
    
    
    };
    #endif
    Last one is my card.h
    Code:
    #ifndef CARD_H
    #define CARD_H
    
    
    
    #include <iostream>
    
    using namespace std;
    
    class Card
    {
    public:
    
        enum Rank
        {
            ACE,
            TWO,
            THREE,
            FOUR,
            FIVE,
            SIX,
            SEVEN,
            EIGHT,
            NINE,
            TEN,
            JACK,
            QUEEN,
            KING
        };
    
        enum Suit
        {
            SPADES,
            HEARTS,
            CLUBS,
            DIAMONDS
        };
    
        Card(); // Default constructor will initialize to Ace of Spades
        Card( Rank newRank, Suit newSuit );
    
        Rank getRank() const {return rank;}
        Suit getSuit() const {return suit;}
    
        void setRank( Rank newRank );
        void setSuit( Suit newSuit );
    
    private:
    
        Rank rank;
        Suit suit;
    };
    
    ostream& operator<<( ostream& os, const Card& card );
    
    #endif;
    Do i code it incorrectly inside the rowstackTest.cpp?
    Can anyone tell me what's going on?

    Thanks

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    1 & 2: faceDownCards is a private member of the class; you called the object MyRowstack
    3: Deck is a type; MyDeck is the object you created

    (These are all in your main function -- on the line inside the for loop.)
    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
    I have remodified my code like that:
    However it has 6 errors when i tried to compile it.
    Here are the error code:
    Compiling...
    rowstack.cpp
    c:\lab8\rowstack.cpp(13) : error C2039: 'push_back' : is not a member of 'stack<class Card,class std::deque<class Card,class std::allocator<class Card> > >'
    c:\lab8\rowstack.cpp(20) : error C2065: 'empty' : undeclared identifier
    c:\lab8\rowstack.cpp(22) : error C2653: 'RowStackExceptions' : is not a class or namespace name
    c:\lab8\rowstack.cpp(22) : error C2065: 'RowstackEmpty' : undeclared identifier
    c:\lab8\rowstack.cpp(24) : error C2065: 'popFaceDownCards' : undeclared identifier
    c:\lab8\rowstack.cpp(24) : error C2228: left of '.top' must have class/struct/union type
    Error executing cl.exe.

    Rowstack.exe - 6 error(s), 0 warning(s)

    and this is the code that I have modified
    Code:
    #include <iostream>
    #include <stack>
    #include "rowstack.h"
    #include "deck.h"
    #include "card.h"
    
    using namespace std;
    
    int main()
    {
    	Deck MyDeck;     // Instantiate a Deck object;
    
    	Rowstack MyRowstack;  // Instantiate a Rowstack object;
    	
    	for(int iCtr = 0; iCtr < 6; iCtr++)
    	{
    		MyRowstack.pushFaceDownCard(MyDeck[iCtr]);
    	}
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I have modified the rowstack.cpp again and I ignored the exception part.
    Code:
    //rowstack.cpp
    #include <cstdlib>
    #include <stack>
    #include <iostream>
    #include "rowstack.h"
    #include "deck.h"
    #include "card.h"
    
    using namespace std;
    
    void Rowstack::pushFaceDownCard(const Card& card)
    {
    	faceDownCards.push(card); //Adds a card to the Rowstack.
    }
    
    
    
    Card Rowstack::popFaceDownCard()
    {
    //	if ( empty() )
    //	{
    //		throw RowStackExceptions::RowstackEmpty;
    //	}
    	 return faceDownCards.top(); // returns the card at the top of the stack.
    }
    
    
    
    bool Rowstack::isFaceDownEmpty() const
    {
    	return faceDownCards.empty();  // returns true if the stack containing the face down cards is Empty
    
    }
    It works afterward.

    If I keep compiling the exception part
    it has 3 errors
    --------------------Configuration: Rowstack - Win32 Debug--------------------
    Compiling...
    rowstack.cpp
    c:\lab8\rowstack.cpp(20) : error C2065: 'empty' : undeclared identifier
    c:\lab8\rowstack.cpp(22) : error C2653: 'RowStackExceptions' : is not a class or namespace name
    c:\lab8\rowstack.cpp(22) : error C2065: 'RowstackEmpty' : undeclared identifier
    Error executing cl.exe.

    Can someone tell me what's going on for the empty?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    if ( empty() )
    return faceDownCards.empty();
    Where did you define a function called empty()?

    c:\lab8\rowstack.cpp(22) : error C2653: 'RowStackExceptions' : is not a class or namespace name
    c:\lab8\rowstack.cpp(22) : error C2065: 'RowstackEmpty' : undeclared identifier
    I don't think your understanding of functions, classes, member variables, and member functions is advanced enough yet to address those errors.
    Last edited by 7stud; 05-08-2005 at 06:17 PM.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Can you give me some advice?
    My idea is :
    Code:
    If (the object is empty)
    {
            throw an exception
    }
    else
         returns the card at the top of the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM