What could be wrong with this?

I've used this same line elsewhere in my program, but here it says that

'Deck' has no member named 'begin'

1
2
3
4
5
6
#include <list>

void Player::get_card(Deck& my_deck, size_t hand_size) 
{
    list<Card>::iterator it = my_deck.begin();
}


AAAAGH! (thanks)

my_deck is not a list<Card>, it's a Deck.

Unless you gave your Deck class a member function called 'begin' that returns an iterator, that won't work.
Thanks. I'm confused, though.

I agree that my_deck is a Deck. However, the Deck consists of a list<Card> doesn't it? I used this exact same line in the Deck class (list<Card>::iterator it = my_deck.begin();) to iterate through a list of Cards in the Deck there. Just to make sure that we are on the same page, the begin() that I am trying to call isn't a member function, per se, but rather part of std::list.

Ultimately, I am trying to pass a Deck of Cards to the Player function to allow the Player to draw cards from the Deck. Will what I am doing work (with some changes)? Or do I need to approach this differently?

Thanks for your help. I appreciate it.
Can you show us your code for the class Deck ?
@OP: This is a good opportunity to say "Yes, but then I'd have to kill you..."

You probably need this -> my_deck.my_deck.begin();.

EDIT: The following post is also probably worth reading.

EDIT2: Also, my suggestion probably won't work, as my_deck is a private member variable...
Last edited on

I agree that my_deck is a Deck. However, the Deck consists of a list<Card> doesn't it?


That doesn't matter. The dot operator works like this:

object.Func();

'object' is an instance of some class (in this case, Deck). The above code tells to compiler to look in Deck for a function called "Func" and call it.

It does not look at each member of Deck to see if those objects have a Func. Doing that wouldn't make any sense and would be problematic anyway.

I used this exact same line in the Deck class (list<Card>::iterator it = my_deck.begin();


In that situation, 'my_deck' must have been a list. In which case it works because list has a member function called 'begin'

Ultimately, I am trying to pass a Deck of Cards to the Player function to allow the Player to draw cards from the Deck. Will what I am doing work (with some changes)? Or do I need to approach this differently?


I would approach it differently. Outside classes/code should not know or care whether or not Deck contains a list. Deck should be the only part of your program that knows how it works.

You should create member functions that the rest of your program to use to work with the Deck.

In this case, it would make sense to have a "Draw" member function:

1
2
// in your Deck class:
  Card Draw();


Then when the player needs to draw some cards:

1
2
3
4
5
6
7
void Player::DrawAHand(Deck& deck)
{
// draw a hand of 5 cards
//  assume 'my_hand' is a vector<Card>
  for(i = 0; i < 5; ++i)
    my_hand.push_back( deck.Draw() );
}


Once you get the idea of how to write code this way, it makes it a lot easier to read and understand. Without even seeing the Deck class or Draw function, you already know what it does and how to use it.

EDIT: revised the DrawAHand example to make more sense
Last edited on
That makes sense to me (more or less). A couple of things that I need to handle, though.

1. I need to manage the Deck. That means that when a player draws a card, it goes into his hand (as you show), but it also comes out of the deck.

2. I HAVE to use std::list to manage everything, even my_hand. This means no arrays.

So, I tried to use your suggestion in light of these requirements. I came up with the following (no surprise, it doesn't work!):

1
2
3
4
//in the Deck class
  Card Draw {
     my_deck.pop_front();
   }


And then in the Player class:
1
2
3
4
5
6
7
    void Player::get_card(Deck& my_deck, size_t hand_size) 
    {
        for (int i = 0; i < hand_size; i++)
        {   
            my_hand.push_back(deck.Draw());
        }
    }


The error that I get is that my_deck was not declared in this scope, which I'm not surprised by. I need to 1) figure out what the top card in the deck is, 2) add it my_and and 3) pop it off the deck.

Any thoughts? Again, I appreciate your help. I'm slowly starting to figure out how to put these classes together. Thanks!
1
2
3
4
//in the Deck class
  Card Draw {
     my_deck.pop_front();
   }


This successfully removes the Card from the top of the Deck, but you're not giving it back to the player. In order to do that, you will have to actually return a Card.

See these two functions:

http://cplusplus.com/reference/stl/list/front/
http://cplusplus.com/reference/stl/list/pop_front/
Topic archived. No new replies allowed.