473,807 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem using vector.earse( ) in a class

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_i terator<_Ty,_Al loc>
std::vector<_Ty >::erase(std::_ Vector_iterator <_Ty,_Alloc>) ' : cannot
convert parameter 1 from 'card' to 'std::_Vector_i terator<_Ty,_Al loc>'
1> with
1> [
1> _Ty=card,
1> _Alloc=std::all ocator<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_car d()
{
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
6 3040
na***********@g mail.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_i terator<_Ty,_Al loc>
std::vector<_Ty >::erase(std::_ Vector_iterator <_Ty,_Alloc>) ' : cannot
convert parameter 1 from 'card' to 'std::_Vector_i terator<_Ty,_Al loc>'
1> with
1> [
1> _Ty=card,
1> _Alloc=std::all ocator<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_car d()
{
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
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
na***********@g mail.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
<na***********@ gmail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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.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
"red floyd" <no*****@here.d ude> wrote in message
news:mY******** **********@news svr25.news.prod igy.net...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2212
by: Eric | last post by:
I am converting my VC++ MFC to Manage C++ and I am having trouble using the vector class. Here is my __gc class ....other headers for System classes .... #include <algorithm> #include <vector> using namespace std;
0
1177
by: menkaur | last post by:
I was writing simple test program for using class libraries (OS: Windows 2003 Server). The problem I’ve got: runtime error: ------------------------------------------- An unhandled exception of type 'System.Runtime.Remoting.RemotingException' occurred in mscorlib.dll Additional information: Remoting configuration failed with the exception System.IO.FileNotFoundException: File or assembly name System.Runtime.Remoting, or one of its...
1
1115
by: James | last post by:
Hi All, Just going into the world of Multithreading and have a few questions about using class's in them. If I create a calls in the main thread then start a new thread that calls another class can i use the class that i created in the main thread? It does work but not sure if this is the correct way to do it. Or should I create a new class in the class that was created by the new
6
1346
by: Martin S. | last post by:
the class: public __gc class Vector { private: Double y, x, z; public: Vector() { y(0),
12
2193
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum. The size of the vector went from 3 (RGB) to 151 (400 nm to 700 with a sample every 2nm). The variables are using a simple Vector class defined as follow: template<typename T, int Depth> class Vector
5
2785
by: Jazi | last post by:
I am newbie to c++ and I would like to build a vector class that returns a vector of objects. I have done some thing like this: student class that contains some of student's properties such as name, address, phone etc. I also have a registered students class which I like to have it as a vector of student objects. The way I have done it so far is like this. RegisteredStduents class{ RegisteredStudents(no_of_students); ...
2
1670
by: curious2007 | last post by:
Hi everyone, This is a code from Numerical Recipes book. It the vector class. However, I am a little confused about how this works. template <class T> class NRVec { private: int nn; //size of array. upper index is nn-1
7
2465
by: sidewinder | last post by:
I wish to use a C++ vector class, which I have taken from the book numerical recipes. However due to the nature of the calculations I wish to perform I need to be able to delete the vectors. I thought that the line ~VEC<double>(a); would do this however when trying to compile using g++ I get the error "no match for ‘operator~’ in ‘~VEC<double>(((const VEC<double>&)((const VEC<double>*)(& a))))" I am sorry this is a bit of a newbie question,...
2
1862
yue219
by: yue219 | last post by:
Im having trouble with this one. I need to create a grid using class, but not sure how to write the code. Please i just need hints =] //array size for Template const int R = 4; const int C = 3; //This is what the player sees upon starting the game. char Template;
5
2330
by: divyasrinivasan | last post by:
hi how to add MULTIPLE TEXTBOX at runtime using CLASS FILE i tried using this code for creating textbox but it did not work textbox t=new textbox(); t.id="mytextbox"; panel p=new panel(); p.controls.add(t); i want it using class file pls help its very urgent...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.