Original Post
Hi I have a header file, implementation file & a main file for a blackjack game but I am getting a compiler error & I cant for the life of me figure out what is wrong with the program. The compiler says that the error occurs in the implementation file where the constructor is defined (line 14): Error is: "New type may not be defined in a return type. Return type specification for constructor invalid." Header file: Main:
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
#include <algorithm>
#include "blackjack.h"
controller::controller() // ERROR occurs here
{
createDeck();
shuffleDeck();
}
void controller::debugShow()
{
// Post: used for debugging; will show remaining cards in deck
for (int i=0; i<deck.size(); i++) {
cout << deck.at(i)->name << " ";
if (i%5==0) { cout << endl; }
}
}
void controller::createCard(string face,char suit,int value)
{
// Post: Create a card & define its face, suit & value
card *newc = new card;
newc->name = face;
newc->suit = suit;
newc->value = value;
deck.push_back(newc); // add card to deck
}
void controller::createDeck()
{
// Post: Create a deck of cards with correct amount & types of cards
char suits[4] = {'c','d','h','s'}; // clubs,diamonds....
string names[14] = {"one","two","three","four","five","six","seven","eight","nine",
"ten","ace","jack","queen","king" };
// build deck of 52 cards
for (int i=0; i<4; i++) {
for (int values=0; values<14; values++) {
createCard(names[values],suits,values+1);
}
}
}
void controller::shuffleDeck()
{
// Post: Shuffle cards in deck
random_shuffle(deck.begin(),deck.end());
}
int controller::deal(hand &myHand)
{
// Pre: Deck MUST have been shuffled
// Post: Deals cards & returns the remaining cards in deck
// shuffleDeck();
card *drawnCard = deck.back(); // WARNING THIS CARD MAY BE DESTROYED WHEN WE POP DECK??
deck.pop_back(); // remove card from deck
myHand.myCards.push_back(drawnCard);
}
void controller::destroyHand(hand &myHand)
{
// Post: Destroys hand to play again
int size = myHand.myCards.size();
for (int i=0; i<size; i++) {
card *temp = myHand.myCards.at(i);
deck.push_back(temp); // put card back into deck
myHand.myCards.pop_back(); // remove card from our hand
// Alternate way: Just push cards back onto deck
// card *temp = myHand.myCards.at(i);
// delete *temp; // delete card
// myHand.myCards.pop_back(); // delete record of card
}
}
#ifndef BLACKJACK_H
#define BLACKJACK_H
#include <iostream>
using namespace std;
#include <windows.h>
#include <string>
#include <vector>
#include <cstdlib>
struct card
{
string name; // one, jack, queen, ace, ten...
int value;
char suit; // diamonds,spades,hearts or clubs (c,d,h,s)
};
struct hand
{
// Our hand we are dealt
vector <card*> myCards;
int total;
};
class controller
{
public:
controller();
void debugShow();
void createCard(string face,char suit,int value);
void createDeck();
void shuffleDeck();
int deal(hand &myHand);
void destroyHand(hand &myHand);
private:
vector <card*> deck; // 52 cards
}
#endif
#include <cstdlib>
#include <iostream>
#include "blackjack.h"
//using namespace std;
int main()
{
controller control;
system("PAUSE");
return 0;
}