Thread: A qustion about Vectors with constructors.

  1. #16
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by joenching
    Hey, if i want to know which header file is required for the method random_shuffle?

    Which homepage provide this imformation?

    Thanks again.
    You need to include the <algorithm> header.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    <algorithm> is the required header. As for which page tells you that...

    http://cppreference.com/
    This one apparently doesn't document the algorithm header.

    http://www.sgi.com/tech/stl/
    This one is ONLY about the STL.

    http://msdn.microsoft.com/library/default.asp
    One of the most complete ones, but it's a pain to navigate with anything but IE (as if it was impossible to implement a proper tree view in Gecko ...), quite slow and some people don't like it.

    http://gcc.gnu.org/onlinedocs/libstd...3.4/index.html
    Fast, but I don't know how complete or good. In any way, it seems the class list freaks out in my firefox and wanders off the screen.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I have already settled the second problem
    just put #include <algorithm> will fix it.

  4. #19
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    For the first error, can anyone suggest me how i should do?

  5. #20
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by joenching
    However, when i try to compile the cpp file, it has two errors:

    Code:
    Card& Deck::operator[](int index)
    {
      if ( index >= 0 && index < 52 )
        return card[index];
    }
    
    error C2440: 'return' : cannot convert from 'int' to 'class Card &'
            A reference that is not to 'const' cannot be bound to a non-lvalue
    and 1 more i think the reason is i haven't coded the #include <someting> inside the header
    Code:
     random_shuffle(card.begin(), card.end());
    error C2065: 'random_shuffle' : undeclared identifier
    change Card& to int

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or change the card member of Deck to vector<Card>. This probably makes more sense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Thantos:
    After i remodifed the program, it can be compiler, however, it has a warning message:

    Code:
    int Deck::operator[] (int index)
    {
      if ( index >= 0 && index < 52 )
        return card[index];
    }
     warning C4715: 'Deck::operator[]' : not all control paths return a value
    I wanna show you my Card class, I think there should has some interact with card class.
    Here is my card.h
    Code:
    #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 );
    here is card.cpp
    Code:
    #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;
    }
    appericate it

  8. #23
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    CornedBee

    I modified it to that:
    Code:
    vector<Card> card; // STL vector of type card.
    it's almost work, but it still shows a error msg which is same as above when i try to compile it.

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You discared Thantos' comment about doing error handling without ever doing anything like it, and now you're surprised it doesn't work?

    Actually, I'd ditch the range test altogether and use vector::at to access the card, that does the range test and the error handling for you.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by CornedBee
    You discared Thantos' comment about doing error handling without ever doing anything like it, and now you're surprised it doesn't work?

    Actually, I'd ditch the range test altogether and use vector::at to access the card, that does the range test and the error handling for you.
    Doesn't at() throw an exception? If so they might not want to have their program crash just because they gave an invalid range

  11. #26
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    You mean i need to do the error handling?

  12. #27
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well considering you'll have to do something if they give you an index out of range, yeah you'll have to do some error handling. Either as the class developer or as the class user

  13. #28
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    It means there has 52 cards in the Deck class
    if i want to show the 53 card in that class.
    The deck class will throw an error exception?

  14. #29
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Currently it will simply crash. If you use at(), it will throw a std::out_of_range.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM