Thread: array of class object

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    Code:
    deck = {Cards(2,"2h"),Cards(3,"3h")};
    This line is throwing up the errors.

    Code:
    class Cards
    {
    private: char name_of_card [5];
             // Value represents the value of the card, 1 being ace 2 - 10 numbers 11 jack , 12 queen , 13 king unlucky for some
    		 int value;
    public: 
    	Cards(int v, char c []);
    	~Cards(){};
    	char  *get_name();
    	int get_value();
    
    };
    Cards::Cards(int v = 2, char c [5] = "2H")
    {
    	value = v;
    	strcpy(name_of_card , c);
    }
    char * Cards::get_name()
    {
    	return name_of_card;
    }
    int Cards::get_value()
    {
    	return value;
    }
    thats the card class
    figured it would be easier to dechyper why that line is giving errors. Its an array of type class

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It's not possible what you want to do. Arrays of objects are alwais initialized with their default-constructors.
    You have to
    Code:
    Deck::Deck(){    
    	deck[0] = Cards(2,"2h");
            deck[1] = Cards(3,"3h");
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    ah ok thanks alot just wondered if there was a more efficient way of doing that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. An Array of Classes in a different Class
    By mas0nite in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2006, 02:28 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM