• Advertisement
Sign in to follow this  

compiler error-previously defined variable...

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I compile this:
[SOURCE]
#include <cstdlib>
#include <iostream>

#include "hand.h"
#include "blackjack_rules.h"

using namespace std;

int main(int argc, char *argv[])
{
    // insert code for opening graphic here
    
    // opening menu with three choices: play, rules, quit
    
    
    system("PAUSE");
    return 0;
}
[/SOURCE]
I get this error: Compiler: Default compiler Building Makefile: "C:\Program Files\Dev-Cpp\Blackjack Game\Makefile.win" Executing make clean rm -f blackjack.o deck.o blackjack_rules.o aux_rand.o Blackjack.exe g++.exe -c blackjack.cpp -o blackjack.o -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/mingw32" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/backward" -I"C:/Program Files/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/Program Files/Dev-Cpp/include" g++.exe -c deck.cpp -o deck.o -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/mingw32" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/backward" -I"C:/Program Files/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/Program Files/Dev-Cpp/include" g++.exe -c blackjack_rules.cpp -o blackjack_rules.o -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/mingw32" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/backward" -I"C:/Program Files/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/Program Files/Dev-Cpp/include" g++.exe -c aux_rand.cpp -o aux_rand.o -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/mingw32" -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/backward" -I"C:/Program Files/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/Program Files/Dev-Cpp/include" g++.exe blackjack.o deck.o blackjack_rules.o aux_rand.o -o "Blackjack.exe" -L"C:/Program Files/Dev-Cpp/lib" blackjack_rules.o(.bss+0x0):blackjack_rules.cpp: multiple definition of `player_name' blackjack.o(.bss+0x0):blackjack.cpp: first defined here blackjack_rules.o(.bss+0x4):blackjack_rules.cpp: multiple definition of `blackjack_cards' blackjack.o(.bss+0x4):blackjack.cpp: first defined here make.exe: *** [Blackjack.exe] Error 1 Execution terminated the header file is this:
[SOURCE]
//////////////////////////////
//
// Blackjack_rules.h
//
//////////////////////////////

#ifndef GUARD_blackjack_rules
#define GUARD_blackjack_rules


#include <vector>
#include <string>
#include <stdexcept>

#include "card.h"
#include "deck.h"
#include "hand.h"

// typical blackjack values
const int MAX_HAND_VALUE = 21;
const int STARTING_MONEY = 100;

// variables to keep track of betting amounts
static int bet = 0;
static int insurance_bet = 0;
static int money = STARTING_MONEY;

// player name
std::string player_name;


// the display function
std::vector<std::string> display(hand& h, std::string who, bool is_dealer = 0);

// fill deck with cards and make default blackjack deck
const deck fill_deck();
deck blackjack_cards = fill_deck();

// deal two cards to player and dealer
void deal(hand& player, hand& dealer);

// make bet before cards are delt
inline void player_bet(int amount) throw(std::domain_error)
{
       if ((money - amount) >= 0) {
                  money -= amount;
                  bet += amount;
                  }
       else throw std::domain_error("You do not have enough money to bet that high");
}

// dealer's play
void dealer_play(hand& dealer_hand);

// are the first two cards a blackjack?
bool is_blackjack(hand& h);

// possible actions player can take
// analyze player hand and give list of options player can take
// after appropriate input, run specified action
void player_actions(hand& h );

// player actions
void hit(hand& h);
void stand();
void double_down(hand& h);
void split(hand& h);

// check and see if dealer has an ace showing
// allow player to buy insurance
// check if dealer has blackjack and pay 2 to 1
void insurance();

// winning and loosing
// scan hand to test if busted
// scan hand and get value
// determine winner
// pay player if they did win
void payoff_conditions(hand& player, hand& dealer);

// payoff formulas
int winning_payoff(int money_bet);
int insurance_payoff(int money_bet);

#endif
[/SOURCE]
the source file for the header is this:
[SOURCE]
////////////////////////////
//
// Blackjack_rules.cpp
//
// Completed April 24, 2004
//
/////////////////////////////

#include <vector>
#include <string>
#include <stdexcept>
#include <iostream>

#include "card.h"
#include "deck.h"
#include "hand.h"
#include "blackjack_rules.h"

const deck fill_deck() 
{
      // deck to be filled
      deck temp;
      
      for (int i = 3; i != 7; ++i) {
          temp.add(card("Ace", 11, i));
          temp.add(card("King", 10, i));
          temp.add(card("Queen", 10, i));
          temp.add(card("Jack", 10, i));
          temp.add(card("10", 10, i));
          temp.add(card("9", 9, i));
          temp.add(card("8", 8, i));
          temp.add(card("7", 7, i));
          temp.add(card("6", 6, i));
          temp.add(card("5", 5, i));
          temp.add(card("4", 4, i));
          temp.add(card("3", 3, i));
          temp.add(card("2", 2, i)); }
      
      
      return temp;
}


void dealer_play(hand& dealer_hand)
{

     while (dealer_hand.value() != 17) {
           
           // add card if not equal to 17
           dealer_hand.add_card(blackjack_cards.draw());
           std::cout << "Dealer Hits \n";
           display(dealer_hand, "Dealer", true);
           std::cout << "Press Enter to Continue...\n";
           std::cin.get();
           system("cls"); }
     display(dealer_hand, "Dealer", true);
     std::cout << "Dealer Stands with " << dealer_hand.value() << std::endl;
}

bool is_blackjack(hand& h)
{
     if (h.size() == 2)
     return h.value() == MAX_HAND_VALUE;
}

// allow player to play each hand
void player_actions(hand& h)
{
     bool did_stand = false;
     
     do { 
        std::cout << player_name + "'s turn.\n";
        std::cout << "Please choose what you would like to do.\n";
     
        char choice;
        
        // test conditions corresponding to actions and run corresponding actions
        
        // possible to split, double down, stand, or hit
        if (h.size() == 2 && h[0].name == h[1].name) {
           std::cout << "You may:\n1. Hit\n2. Stand\n3. Split\n4. Double Down\n"
                     "Please enter you choice...\n";
           std::cin.get(choice);
           
           bool valid_input = false;
           do {
           switch (choice) {
                  case '1': hit(h); valid_input = true;
                  case '2': stand(); valid_input = true; did_stand = true;
                  case '3': split(h); valid_input = true;
                  case '4': double_down(h); valid_input = true;
                  default: std::cout << "You failed to enter the correct input.\n"
                           "Please try again...";
                  }
           } while(!valid_input);
        }
        // possible to double down, stand, or hit
        else if (h.size() == 2 ) {
           std::cout << "You may:\n1. Hit\n2. Stand\n3. Double Down\nPlease enter "
                     "you choice...\n";
           std::cin.get(choice);
           
           bool valid_input = false;
           do {
           switch (choice) {
                  case '1': hit(h); valid_input = true;
                  case '2': stand(); valid_input = true; did_stand = true;
                  case '3': double_down(h); valid_input = true;
                  default: std::cout << "You failed to enter the correct input.\n"
                           "Please try again...";
                  }
           } while(!valid_input);
        }    
        // possible to hit or stand
        else {
           std::cout << "You may:\n1. Hit\n2. Stand\nPlease enter you choice...\n";
           std::cin.get(choice);
           
           bool valid_input = false;
           do {
           switch (choice) {
                  case '1': hit(h); valid_input = true;
                  case '2': stand(); valid_input = true; did_stand = true;
                  default: std::cout << "You failed to enter the correct input.\n"
                           "Please try again...";
                  }
           } while(!valid_input);
        }   
     } while (h.value() <= MAX_HAND_VALUE || did_stand);
} 

// display function
std::vector<std::string> display(hand& h, std::string who, bool is_dealer)
{
      const std::string SPACE_BETWEEN_CARDS(4, ' ');
      const size_t CARD_HEIGHT = 15;
      const size_t CARD_LENGTH = 9;
      const std::string NEW_LINE = " ";
      const int NUMBER_CARDS = h.size();
      
      
      // temp vector string to put each line of input into
      std::vector<std::string> pic;
      
      // who's hand is being displayed?
      pic.push_back(who + "'s Hand");
      pic.push_back(NEW_LINE);
      
      // display total value of hand
      pic.push_back("Current Value of Your Hand: " + h.value());
      pic.push_back(NEW_LINE);
      pic.push_back(NEW_LINE);
      
      // draw cards on console
      // go through line by line
      for (size_t n = 0; n != CARD_HEIGHT; n++) {
           std::string temp;
           
           // iterate through cards
           for (hand::iterator i = h.begin(); i != h.end(); ++i) {
           
               
               
               // special condition for first card if dealer
               if (i == h.begin() && is_dealer) 
                     temp += std::string(CARD_LENGTH, '*') + SPACE_BETWEEN_CARDS;
                     
               // is the line the first or last line  
               else if (n == 0 || n == CARD_HEIGHT - 1)
                     temp+= std::string(CARD_LENGTH, '*') + SPACE_BETWEEN_CARDS;
                     
               // does the line contain a special suit char
               else if (n == 1 || n == CARD_HEIGHT - 2)
                     temp+= "*" + std::string(1, i->suit) + std::string((CARD_LENGTH - 4), '*') + 
                            std::string(1, i->suit) + "*" + SPACE_BETWEEN_CARDS;
               
               // does the line contain the value of the card
               else if (n == CARD_HEIGHT/2) {
                     // find out the length of the name
                     size_t size_of_name = i->name.size();
                     // width to pad name
                     std::string pad;
                     
                     // may need to add one to the padding on the right side 
                     temp += "*" + pad + i->name;
                     
                     if (CARD_LENGTH % 2 != size_of_name % 2)
                        temp += pad + " *" + SPACE_BETWEEN_CARDS;
                     else temp += pad + "*" + SPACE_BETWEEN_CARDS;
                     }
               
               // any other line
               else
                     temp += "*" + std::string(CARD_LENGTH - 2, ' ') + "*" + 
                          SPACE_BETWEEN_CARDS;
                     
               }
                     
        pic.push_back(temp);
        }
        pic.push_back(NEW_LINE);
        pic.push_back(NEW_LINE);
        return pic;
}

void deal(hand& player, hand& dealer)
{
       player.add_card(blackjack_cards.draw());
       dealer.add_card(blackjack_cards.draw());
       player.add_card(blackjack_cards.draw());
       dealer.add_card(blackjack_cards.draw());
       
       //display cards
       display(dealer, "Dealer", true);
       display(player, player_name);
}

void hit(hand& h)
{
     std::cout << "You Hit.\n";
     h.add_card(blackjack_cards.draw());
}

void stand() 
{
     std::cout << "You stand.\n"; 
}

void double_down(hand& h) 
{ 
     // make sure its possible to double_down
     if (h.size() == 2) {
                  try {
                  std::cout << "You double down.\n";
                  int additional_bet;
                  h.add_card(blackjack_cards.draw());
                  std::cout << "Please enter an amount to bet up to your original bet...\n";
                  std::cin >> additional_bet;
                  while (additional_bet > bet) {
                          std::cout << "Your new bet is too large.  Please enter "
                                    "another bet...\n";
                          std::cin >> additional_bet; }
                  player_bet(additional_bet);}
                  catch (std::domain_error err) {
                        std::cout << err.what() << std::endl;
                        std::cout << "You currently have $" << money << std::endl;
                        // will keep calling double_down on hand until bet is correct
                        double_down(h);
                  }
     }
}
// have two identical cards, put original bet on both, and get a second card for each
// one, now you have two hands
void split(hand& h) 
{
     hand h1;
     hand h2;
     
     std::cout <<"You split.\n";
     if ((h.size() == 2) && ((h[1]).name == (h[0]).name)) { 
           try {player_bet(bet);}
           catch (std::domain_error err) {
                 throw std::runtime_error("Not enough money to split. Choose "
                       "another option...\n");
                 }
           std::cout << "You match your current bet.\n";
           
           // form two new hands to play
           h1.add_card(h[0]);
           h1.add_card(blackjack_cards.draw());
           h2.add_card(h[1]);
           h2.add_card(blackjack_cards.draw());
           
           // play each hand seperately
           player_actions(h1);
           player_actions(h2);
     }
         
}

void insurance(hand& dealer)
{
     //second card will be showing
     if (dealer[1].name == "Ace" && dealer.size() == 2) {
           char choice;
           std::cout << "The dealer has an Ace showing.  Would you like to buy "
                     "insurance?\n1. Yes\n2. No\n";
           std::cin.get(choice);
           
           bool valid_input = false;
           do {
           switch (choice) {
                  case '1':
                       std::cout << "You may purchase insurance up to half of your original bet\n"
                                 "Please enter the amount of insurance you wish to buy\n";
                       std::cin >> insurance_bet;
                       while (insurance_bet > bet/2) {
                             std::cout << "Your insurance amount is to high.\nPlease enter "
                                       "an amount under " << bet/2 << std::endl;
                       }
                       money -= insurance_bet;
                       valid_input = true;
                  case '2': break; valid_input = true;
                  default: std::cout << "Please enter 1 or 2"; }
           } while (!valid_input);
     }    
}


void payoff_conditions(hand& player, hand& dealer)
{
     // the payoff depends on many conditions, most of all is that the player
     // did not bust
     if (player.value() <= MAX_HAND_VALUE) {
     
        // if the player has blackjack, they automatically win
        if (is_blackjack(player)) {
           // even if dealer has blackjack
           if (is_blackjack(dealer))
                 insurance_payoff(insurance_bet);
           winning_payoff(bet);
        }
     
        // if dealer has blackjack
        else if (is_blackjack(dealer))
           insurance_payoff(insurance_bet);
     
        // if player has less than 21 and more than dealer
        else if (player.value() > dealer.value())
             winning_payoff(bet);
             
        // if dealer busts
        else if (dealer.value() > MAX_HAND_VALUE)
             winning_payoff(bet);
        }

}

int winning_payoff(int money_bet)
{
    // pays 3 to 2 for winning
    return 3*(money_bet/2);
}

int insurance_payoff(int money_bet)
{
    // pays 2 to 1 for winning
    return 2*money_bet;
}
[/SOURCE]
Can someone please tell me why im getting the error?

Share this post


Link to post
Share on other sites
Advertisement
You might want to read this article: Organizing Code Files in C and C++.

The short answer is that you have defined those two variables in a header file. You shouldn't do that. When you include that header in each of your .cpp files, the compiler will create those variables in each object file. You can "declare" the variables in the header file, but don't "define" them there. Variable definitions should be in a .cpp file.

Modify the header so that the variables are declared as extern. Then place the variable definitions in one of your .cpp files.

(This might be a good item for the Forum FAQ.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement