By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,061 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,061 IT Pros & Developers. It's quick & easy.

Problem using vector.earse( ) in a class

P: n/a
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.

Apr 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
na***********@gmail.com wrote:
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

Apr 16 '06 #2

P: n/a
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.

Apr 16 '06 #3

P: n/a
na***********@gmail.com wrote:
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
Apr 16 '06 #4

P: n/a
<na***********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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
Apr 17 '06 #5

P: n/a
P.J. Plauger wrote:
<na***********@gmail.com> wrote in message

[redacted]
I can't use a queue, this design must implement vectors only.


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


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

Apr 17 '06 #6

P: n/a
"red floyd" <no*****@here.dude> wrote in message
news:mY******************@newssvr25.news.prodigy.n et...
P.J. Plauger wrote:
<na***********@gmail.com> wrote in message

[redacted]
I can't use a queue, this design must implement vectors only.


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


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


Ah, silly me. I'm getting too credulous in my old age.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Apr 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.