Ask Experts Questions for FREE Help !
Ask
    RobotJones's Avatar
    RobotJones Posts: 4, Reputation: 1
    New Member
     
    #1

    Apr 24, 2007, 01:24 AM
    Using vectors of user-defined types
    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's Avatar
    asterisk_man Posts: 476, Reputation: 32
    Full Member
     
    #2

    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++
    RobotJones's Avatar
    RobotJones Posts: 4, Reputation: 1
    New Member
     
    #3

    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's Avatar
    asterisk_man Posts: 476, Reputation: 32
    Full Member
     
    #4

    Apr 25, 2007, 06:55 AM
    Did you #include the class's header file in the file that contains vector<aCard> ?
    RobotJones's Avatar
    RobotJones Posts: 4, Reputation: 1
    New Member
     
    #5

    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's Avatar
    asterisk_man Posts: 476, Reputation: 32
    Full Member
     
    #6

    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!

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Change the way user log on or off in user account [ 4 Answers ]

Hi, I re-installed my operating system recently . I wanted to change my display screen if I go to the user accounts and click the option which is CHANGE THE WAY USER LOG ON OR OFF it displays a message saying that CLIENT SERVER FOR NETWARE HAS DISABLED THE WELCOME SCREEN AND FAST USER...

Calculating vectors [ 1 Answers ]

Hey I got a question that I am stuck with and need some help on it A boat that is capable of travelling at 4.5km/hr in still water is travelling on a river whose current is 1.5jm/h East. Find the time it takes for the boat to travel 3.0km downstream relative to the shore. And The time the boat...

Calculating vectors [ 5 Answers ]

Hello. I'm having trouble with two questions regarding vectors. I'm not asking for solutions-just a step in the right direction. 1)A skater glides along a circular path of radius 4.90m. a)if he coasta around one half of the circle, find the magnitude of the displacement vector b)find how...

Defined Benefit Plan [ 1 Answers ]

I am 58 years old & recently down sized from my employer of 23 years. I have since started to work for the state. I have a defined benefit plan that offers reduced early retirement at age 55. What would be the penalties / reduced benefits be if I started to receive pension payments now?


View more questions Search