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
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