Problem using vector.earse( ) in a class

N

nabeel.girgis

I'm creating a deck class which uses a card class. The card class is
correct and works perfectly. One fo my member functions is to draw a
card, but I'm having trouble removing the top card after it has been
dealt. I have tested this in a seperate program to see if it work and
it does. But I get this error when I try to compile my deck class:

'std::_Vector_iterator<_Ty,_Alloc>
std::vector<_Ty>::erase(std::_Vector_iterator<_Ty,_Alloc>)' : cannot
convert parameter 1 from 'card' to 'std::_Vector_iterator<_Ty,_Alloc>'
1> with
1> [
1> _Ty=card,
1> _Alloc=std::allocator<card>
1> ] No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called.

This is my class declaration:

class deck1
{
public:
deck1();

bool isEmpty() const;
card draw_card();
void shuffle();

private:
vector<card> deck;
};

This is my draw_card function code.

card deck1::draw_card()
{
card top;


top = deck.at(0);

**** deck.erase(deck.begin());

return (top);
}

When I double click on the error, it brings me to the line with the
***. Thanks.
 
K

Kai-Uwe Bux

I'm creating a deck class which uses a card class. The card class is
correct and works perfectly. One fo my member functions is to draw a
card, but I'm having trouble removing the top card after it has been
dealt. I have tested this in a seperate program to see if it work and
it does. But I get this error when I try to compile my deck class:

'std::_Vector_iterator<_Ty,_Alloc>
std::vector<_Ty>::erase(std::_Vector_iterator<_Ty,_Alloc>)' : cannot
convert parameter 1 from 'card' to 'std::_Vector_iterator<_Ty,_Alloc>'
1> with
1> [
1> _Ty=card,
1> _Alloc=std::allocator<card>
1> ] No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called.

This is my class declaration:

class deck1
{
public:
deck1();

bool isEmpty() const;
card draw_card();
void shuffle();

private:
vector<card> deck;
};

This is my draw_card function code.

card deck1::draw_card()
{
card top;


top = deck.at(0);

**** deck.erase(deck.begin());

return (top);
}

try:

assert( ! deck.empty() );
top = deck.front();
deck.pop_front();

also: you may want to use a deque instead of a vector. That makes dealing
from the front more efficient. Alternatively, you could deal from the back
of the vector:

assert( ! deck.empty() );
top = deck.back();
deck.pop_back();


Best

Kai-Uwe Bux
 
N

nabeel.girgis

Thanks for replying. I know that pop_front() is predefined function of
the vector class, but I tried it anyways and got the error message
saying that it's not declared in the vector class.

I can't use a queue, this design must implement vectors only. Right
now I'm trying an iterator since that is the parameter for the erase ()
function. But I got error messages in the past when I used it.


I just tried the iterator, it compiles that part with any errors. But
I am receiving other errors about functions already having a body. I
posted another thread with that problem a couple of minutes ago.
Thanks for your help.
 
K

Kai-Uwe Bux

Thanks for replying.

a) Please quote to provide some context.

a1) Communication via a news group is public and others may have a reason
to join the conversation at any time. This will usually be for your
benefit as they might provide additional information.

a2) In any case, do not assume that the previous parts of the thread are
available to the readers. For technical reasons posts can appear at
different times on different servers. Therefore it is good practice
on usenet to make each post self-contained.
I know that pop_front() is predefined function of
the vector class, but I tried it anyways and got the error message
saying that it's not declared in the vector class.

Are you using std::vector? May I suggest you post a stripped down piece of
code that (a) compiles out of the box and (b) demonstrates the problem --
in your case that means it should compile with the error message you
encounter prominently displayed.
I can't use a queue, this design must implement vectors only.

Why? Anyway: if you do back() and pop_back(), std::vector will be efficient.

Right now I'm trying an iterator since that is the parameter for the
erase () function. But I got error messages in the past when I used it.

I just tried the iterator, it compiles that part with any errors.
?

But I am receiving other errors about functions already having a body. I
posted another thread with that problem a couple of minutes ago.

Again, a minimal complete piece of code that demonstrates the problem is the
recommended way of communicating these issues. This way, the readers can
cut, paste, compile, and debug your code locally and get back to you with
the analysis.


Best

Kai-Uwe Bux
 
P

P.J. Plauger

Thanks for replying. I know that pop_front() is predefined function of
the vector class,

Gee, *I* don't know that.
but I tried it anyways and got the error message
saying that it's not declared in the vector class.

And evidently your compiler doesn't know that either.
I can't use a queue, this design must implement vectors only.

What an interesting way to design software. Implementation first,
requirements afterward.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
R

red floyd

P.J. Plauger said:
What an interesting way to design software. Implementation first,
requirements afterward.

Dr. Plauger, this artificial requirement makes it obvious it's homework.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top