-
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?
-
Code:
class Deck
{
private:
Cards deck[2];
public:
Deck();
~Deck(); // <-----
};
Deck::Deck()
{
deck = {Cards(2,"2h"),Cards(3,"3h")};
}
-
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:
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
-
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
-
ah ok thanks alot just wondered if there was a more efficient way of doing that