0

I am trying to create a deck of cards by creating a "card deck" class which uses an array of 52 "card" class objects. The array needs to be dynamically allocated, but I can't figure out the syntax to create this. My code gives this error:

error C2512: 'Card' : no appropriate default constructor available

#include "CardDeck.h" 
#include "Card.h"
#include <iostream>      
#include <cstdlib>       
using namespace std;

CardDeck::CardDeck()
{

    *Deck = new Card[52];

} 

I am curious as to whether I am able to create the array using my Card::Card(char a , char b) constructor, or if I must first create the array using a default constructor.

6
  • 'Card' : no appropriate default constructor available - this means you don't have a constructor of the form Card::Card(). Commented May 3, 2013 at 6:14
  • 1
    Wow, for starters you definitely should not be including Card.cpp Commented May 3, 2013 at 6:14
  • "error C2512: 'Card' : no appropriate default constructor available" - hmm... what could that cryptic error possibly mean? Commented May 3, 2013 at 6:15
  • it seems like your Card class do not have a constructor Card(). Please include declaration of class Card also. Commented May 3, 2013 at 6:15
  • Whoops. I should have mentioned that the card class I want to use uses two char parameters. I'm just not sure the syntax of how to write this. Commented May 3, 2013 at 6:18

2 Answers 2

2

To create an array of cards like that Card must have a default constructor

class Card
{
public:
    Card(); // default cosntructor
    ...
};

The reason is that a default constructor is needed to give the initial values to your array.

As juanchopanza says you should be using a vector instead, but you would still have the same problem.

As Ben says #include "Card.cpp" is also wrong.

Also this line looks suspicious

*Deck = new Card[52];

why are you dereferencing Deck? Almost certainly that is wrong too. I would show some more code. Seems you have quite a few errors.

Sign up to request clarification or add additional context in comments.

Comments

2

It isn't clear that it has to be dynamically allocated, but you can simplify the problem by using an std::vector<card> instead:

// CardDeck.h
#include <vector>

class CardDeck
{
 public:
  CardDeck() : deck_(52) {} // initialie deck_ to hold 52 cards
 private:
  std::vector<Card> deck_;
};

Don't forget to add include guards and do not include .cpp files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.