Skip to main content
GameDev.net gamedev.net
🔒 Locked

Constructor Error I cant find

Started by gretty Jan 16, 2010 at 10:06 PM 2 replies 1k views
Original Post
gretty
gretty
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."

#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
     }
}

Header file:

#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 


Main:

#include <cstdlib>
#include <iostream>

#include "blackjack.h"

//using namespace std;

int main()
{
    controller control;
    
    system("PAUSE");
    return 0;
}


Erik Rufelt
Erik Rufelt
You're missing a semicolon after the terminating }, in the declaration of the controller class. It should be };, as you have for your structs.
terloon
terloon
I'm not sure if this is it, but I think you're missing a semicolon at the end of your class definition.
towerofinfinity.blogspot.comgamecodemonkey.blogspot.com
gretty
gretty
Thanks :D It's amazing how something so simple & little can stuff everything up :P

Thanks again

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.