Log in

View Full Version : Using vectors of user-defined types


RobotJones
Apr 24, 2007, 01:24 AM
I'm a beginner amateur programmer and I'm trying to write a module for card games. I know that several already exist, but I thought it would be great practice to create my own. My problem arises when I try to create a vector of a user-defined type instead of a normal type, like int or float. I thought that this was possible but I keep getting compiler errors. I'll give you a description of what I've done followed by the code itself and finally the errors.

I made two enumerations, 'aRank' and 'aSuit'. Then I made a class called 'aCard' with two members, one 'aRank' and one 'aSuit'. (The class also has methods and overloaded operators but are of no significance). Then I declare a global variable called 'deck' of type 'vector<aCard>'. And finally I define a function called 'initDeck()' which is supposed to initialize 'deck'. Here's the relevant code:
------------------------------------------------------------------------------------------------------
enum aRank { TWO=2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
enum aSuit { HEARTS, DIAMONDS, CLUBS, SPADES };

class aCard
{
public: // constructors, destructors, overloaded operators, methods,.
private:
aRank itsRank;
aSuit itsSuit;
};

vector<aCard> deck; // global variable

void initDeck()
{
deck.clear();
for(aSuit suit=HEARTS; suit<=SPADES; suit++)
{
for(aRank rank=TWO; rank<=ACE; rank++)
{
aCard card(rank,suit);
deck.push_back(card);
}
}
}
----------------------------------------------------------------------------------------------------
Here are the errors I get when I try to compile (I use Borland C++BuilderX Personal):
----------------------------------------------------------------------------------------------------
"_vector.h": E2450 Undefined structure 'aCard' in function _Vector_base<aCard,allocator<aCard> >::~_Vector_base() at line 83

"_vector.h": E2453 Size of the type 'aCard' is unknown or zero in function _Vector_base<aCard,allocator<aCard> >::~_Vector_base() at line 83

"_alloc.h": E2450 Undefined structure 'aCard' in function allocator<aCard>::deallocate(aCard *,unsigned int) const at line 360

"_alloc.h": E2109 Not an allowed type in function allocator<aCard>::deallocate(aCard *,unsigned int) const at line 360

"_construct.h": E2450 Undefined structure 'aCard' in function _STL::void __destroy_aux<aCard *>(aCard *,aCard *,const __false_type &) at line 106

"_construct.h": E2453 Size of the type 'aCard' is unknown or zero in function _STL::void __destroy_aux<aCard *>(aCard *,aCard *,const __false_type &) at line 106
-----------------------------------------------------------------------------------------------------


So my question is this: is there some way I can have a vector of a user-defined type such as my 'aCard'? And if so, how do I do it? Thank you in advance for your answers.

RobotJones

asterisk_man
Apr 24, 2007, 06:18 AM
I don't think that there's any reason why you can't have a vector of a user-defined type.
It's hard to debug your errors because I don't know what your line numbers are.
Also, I think in your initDeck() function you should only declare card once outside of the for loops. I think push_back will make a copy of the data. Do you have anything in aCard that would require a copy constructor or assignment operator?

You can see a very similar question with some examples here: Can <list> be applied to user-defined class in C++? - C++ (http://www.thescripts.com/forum/thread168484.html)

RobotJones
Apr 24, 2007, 11:03 PM
First of all, I thank you SO much for your reply. I wasn't sure how long it would take to get an answer, if I was to get one at all.

As far as the first comment about not being able to debug easily because of the lack of line numbers, none of the six errors I got came from anything I coded. The line numbers are from included files, namely: "_vector.h", "_alloc.h", and "_construct.h". I only included two files: <vector> and <algorithm>. I guess the other files are includes of <vector> itself.

And you were totally correct in suggesting to declare 'card' only once outside of my for loops. That was just bad form on my part. But, unfortunately, after I made the changes I still got the same six errors as before.

And finally, you asked if my 'aCard' class had anything in it requiring a copy constructor or an assignment operator. Here is the actual 'aCard' class the way I wrote it:

-----------------------------------------------------------------------------------------------------
class aCard
{
public:

// default constructor & destructor
aCard() { }
~aCard() { }

// overloaded constructor with initializations
aCard(aRank theRank, aSuit theSuit):itsRank(theRank), itsSuit(theSuit) { }

// overloaded operators
bool operator==(const aCard &b)
{
if(itsRank == b.itsRank) return true;
else return false;
}
bool operator!=(const aCard &b)
{
if(itsRank != b.itsRank) return true;
else return false;
}
bool operator>(const aCard &b)
{
if(itsRank > b.itsRank) return true;
else retrun false;
}
bool operator<(const aCard &b)
{
if(itsRank < b.itsRank) return true;
else return false;
}
bool operator>=(const aCard &b)
{
if(itsRank >= b.itsRank) return true;
else return false;
}
bool operator<=(const aCard &b)
{
if(itsRank <= b.itsRank) return true;
else return false;
}
void operator=(const aCard &b)
{
itsRank = b.itsRank;
}

// public setter methods
void setRank(aRank theRank) { itsRank = theRank; }
void setSuit(aSuit theSuit) { itsSuit = theSuit; }
void setCard(aRank theRank, aSuit theSuit)
{
itsRank = theRank;
itsSuit = theSuit;
}

// public getter methods
aRank getRank()const { return itsRank; }
aSuit getSuit()const { return itsSuit; }

private:
// private data members
aRank itsRank;
aSuit itsSuit;
};
-----------------------------------------------------------------------------------------------------

So there it is. I tried taking out the overloaded assignment operator and even the overloaded constructor but it did nothing to get rid of those errors. To tell you the truth, I've read about copy constructors once but didn't quite grasp it the first time. I've been meaning to go back over it again but I just haven't gotten around to it yet. So just to be safe I tried taking out the overloaded constructor also, thinking it might be a copy constructor, but again, no change in errors.

Well, if there's anything else you can think of I'd greatly appreciate the input. Again, thanks a bunch.

asterisk_man
Apr 25, 2007, 06:55 AM
Did you #include the class's header file in the file that contains vector<aCard> ?

RobotJones
Apr 25, 2007, 08:42 PM
Oh, I feel like such an idiot! I had the #include in the wrong stupid file! Here I thought I was actually making progress as a programmer, defining my own classes and creating vectors of a user-defined type, and then I do something stupid like THAT. I'm sorry to have wasted your time on this NON-problem. I feel like the guy who paid $50 an hour for a repairman to tell him he didn't have the washing-machine plugged in!

Well, thanks again for the help. I hope you're around the next time I have a problem. Hopefully it will be a more intelligent problem.

asterisk_man
Apr 26, 2007, 06:01 AM
Don't sweat it. Everyone's had similar problems. Now that you've seen it and debugged it once you're less likely to do it in the future.

I think that the error "Undefined structure 'aCard'" would be the clue to this problem. If I see "undefined blah" I think, why isn't blah defined? Did I spell/capitalize incorrectly? Is it out of scope? Is it defined too late?

I hope the rest of your project proceeds well. Let us know if you have any other questions!