Thread: array of class object

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7

    array of class object

    i'm creating an array of a class object. The class in question has default values and takes two arguments, an in and a char array.

    Code:
    class Deck
    {
    private:
    	Cards deck[2];
    public:
    	Deck();
    	~Deck(){};
    };
    Deck::Deck()
    {
    	deck = {Cards(2,"2h"),Cards(3,"3h")};
    }
    thats the class which creates an array of the class Cards.

    However this is throwing up:

    error C2059: syntax error : '{'
    error C2143: syntax error : missing ';' before '{'
    error C2143: syntax error : missing ';' before '}'

    Is my syntax wrong somehow i can't see it?
    Last edited by TomButcher; 09-03-2005 at 06:49 AM.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    class Deck
    {
    private:
    	Cards deck[2];
    public:
    	Deck();
    	~Deck();    // <-----
    };
    Deck::Deck()
    {
    	deck = {Cards(2,"2h"),Cards(3,"3h")};
    }

  3. #3
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    If you want to define function body outside class, you first must make prototype inside class.
    Code:
    class Deck
    {
    ...
    ~Deck();    //    this is prototype, its body is defined elsewhere.
                     //         Don't forget the semicolon.
    };
    
    Deck::~Deck()
    {}
    If you don't need to do anything in functions body, you can define its body inside class.
    Code:
    class Deck
    {
    ...
    ~Deck(){}    //    don't put semicolor after destructors body
    };
    Code:
     
         .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW=	
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  4. #4
    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

  5. #5
    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

  6. #6
    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