Strange bracket mismatch undefined class error

This is a discussion on Strange bracket mismatch undefined class error within the C++ Programming forums, part of the General Programming Boards category; I've been checking my code over and over and over again, I'm missing something, perhaps someone with an outside view ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Strange bracket mismatch undefined class error

    I've been checking my code over and over and over again, I'm missing something, perhaps someone with an outside view on the issue can find something, here is the code causing the problem (i think).

    Code:
    #ifndef DEALER_H
    #define DEALER_H
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include "Deck.h"
    #include "Human_Player.h"
    #include "Player.h"
    
    class Dealer : public Player
    {
    public:
    
    	Dealer()
    	{
    		playerid = 0;
    		InitLookups();
    	}
    	void ShuffleDeck()
    	{
    		random_shuffle(deck.cards.begin(), deck.cards.end());
    	} 
    
    	int Turn(Human_Player & player)
    	{
    		int result = player.GetInput();
    		if (result == 1)
    		{
    			Deal(1, player);
    			return 1; // is player's hand > 21?
    		}
    		if (result == 2)
    		{
    			return 2; //compare cards declare winner
    		}
    		if (result == 3)
    		{	
    			return 3; //player folded, remove player from game.
    		}
    		result = player.GetInput();
    	}
    
    	void Deal(int howmanycards, Player & player)
    	{
    		if {player.playerid == 0)
    		{
    			std::cout << "Dealer's Cards" << std::endl;
    			std::cout << "--------------" << std::endl;
    			for (int loop = 0; loop != howmanycards; loop++) 
    			{
    				player.hand.push_back(deck.cards.back());
    				if (loop == 2)
    				{
    					std::cout << "Card" << std::endl;	
    					deck.cards.pop_back();
    				}
    				else
    				{
    				ReadCards(hand.back().Value, hand.back().Suit);
    				deck.cards.pop_back();
    				}
    			}
    		}
    		if (player.playerid !=0)
    		{
    			for (int loop = 0; loop != howmanycards; loop++) 
    			{
    				hand.push_back(deck.cards.back());
    				ReadCards(hand.back().Value, hand.back().Suit);
    				deck.cards.pop_back();
    			}
    		}
    	}
    
    	void ReadCards(int value,int suit)
    	{
    		std::cout << valuelookup[value].c_str() << " of "
    			 << suitlookup[suit].c_str() << std::endl;
    		//card value and suit as an index...
    	}
    
    	// Creates Lookup tables for outputting card suits and values
    	void InitLookups()
    	{
    		string suit;
    		suit = "Clubs"; Add(suit);
    		suit = "Diamonds"; Add(suit);
    		suit = "Hearts"; Add(suit);
    		suit = "Spades"; Add(suit);
    
    		string value; 
    		value = "Two"; Add2(value);
    		value = "Three"; Add2(value);
    		value = "Four"; Add2(value);
    		value = "Five"; Add2(value);
    		value = "Six"; Add2(value);
    		value = "Seven"; Add2(value);
    		value = "Eight"; Add2(value);
    		value = "Nine"; Add2(value);
    		value = "Ten"; Add2(value);
    		value = "Jack"; Add2(value);
    		value = "Queen"; Add2(value);
    		value = "King"; Add2(value);
    		value = "Ace"; Add2(value);
    	}
    private:
    
    	void Add(string suit)
    	{
    		suitlookup.push_back(suit);
    	}
    
    	void Add2(string value)
    	{
    		valuelookup.push_back(value);
    	}
    	//string lookups
    	vector<string> suitlookup;
    	vector<string> valuelookup;
    	vector<Card> hand;	
    	Deck deck;
    	friend class Game;
    };
    #endif
    Here is the error:
    Code:
    ------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
    Compiling...
    Blackjack.cpp
    c:\documents and settings\jon c\desktop\blackjack\blackjack\game.h(9) : error C2079: 'Dealer::Game::dealer' uses undefined class 'Dealer'
    c:\documents and settings\jon c\desktop\blackjack\blackjack\blackjack.cpp(15) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\jon c\desktop\blackjack\blackjack\dealer.h(11)' was matched
    Build log was saved at "file://c:\Documents and Settings\Jon C\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    It's rediculous, where do I ever say dealer::game::dealer?
    And as far as I can tell all my brackets and semicolon's are in place?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Erm, I believe you have a declaration in the .h file? Because the "undefined" comes from the .h file. Maybe you simply misspelled it? I can't say.
    Your definition is totally wrong (or is this your declaration & you're defining inline?).
    It should be
    Code:
    Dealer::Dealer()
    Code:
    void Dealer::InitLookups()
    etc.

    OH yes, and you use IF, then IF, but not ELSE IF.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    maybe I should post more code?

    In the Deal function what exactly is wrong with the if's and else's? I've went over the logic a hundred times but I still can't see the problem, I know this is the function with the error because thats the function I was working on when I compiled and alluva sudden had this error.

    Game.h
    Code:
    #ifndef GAME_H
    #define GAME_H
    #include "Dealer.h"
    #include <vector>
    #include <iostream>
    class Game
    {
    public:
    	Dealer dealer;
    	Human_Player player;
    	 void playgame()
    	 {
    		 InitValues();
    		 player.playerid = 1;
    		 dealer.ShuffleDeck();
    		 dealer.Deal(2, dealer);
    		 dealer.Deal(1, player);
    		 int gameloop = 1;
    		 while(gameloop != 0)
    		 {
    			 gameloop = BlackJack(dealer.Turn(player));
    		 }
    		  
    	 }
    	 int BlackJack(int option)
    	 {
    		 if (option == 1)	// check player hand, over 21?
    		 {
    			 if (Hand(player.hand) > 21)
    			 {
    				std::cout << "You Lose!!!!" << std::endl;
    				return 0;
    			 }
    			 else { return 1; }
    		 }
    		 if (option == 2)	// compare hands and declare winner
    		 {
    			 if(Hand(player.hand) > Hand(dealer.hand))
    			 {
    				 std::cout << "You win\n!!!!";
    				 return 0;
    			 }
    			 else { std::cout << " You Lose\n!!!"; return 0; }
    		 }
    		 if (option == 3)	// player folds, remove them from game.
    		 {
    			 std::cout << "You Lose!!!\n";
    			 return 0;
    		 }
    		 else{ return 0; }
    	 }
    
    	 int Hand(vector<Card> & hand)
    	 {
    		 sum = 0; int loop = 0;
    		 for (loop; loop < hand.size(); loop++)
    		 {
    			 sum += values[hand[loop].Value];
    		 }
    		 return sum;
    	 }
    	 void InitValues()
    	 {
    		values.push_back(2);
    		values.push_back(3);
    		values.push_back(4);
    		values.push_back(5);
    		values.push_back(6);
    		values.push_back(7);
    		values.push_back(8);
    		values.push_back(9);
    		values.push_back(10);
    		values.push_back(10);
    		values.push_back(10);
    		values.push_back(10);
    		values.push_back(11);
    	 }
    	 int sum;
    	 vector<int> values;
    };
    #endif
    That code will let you know how the Dealer class is used. I'm pretty proud of my setup here lol.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by Shamino View Post
    maybe I should post more code?
    That code will let you know how the Dealer class is used. I'm pretty proud of my setup here lol.
    OK, so game.h uses Dealer declared in Dealer.h and includes it. So what's in Deadler.h?

    Quote Originally Posted by Shamino View Post
    In the Deal function what exactly is wrong with the if's and else's? I've went over the logic a hundred times but I still can't see the problem, I know this is the function with the error because thats the function I was working on when I compiled and alluva sudden had this error.
    For example:
    Quote Originally Posted by Shamino View Post
    Code:
    	int Turn(Human_Player & player)
    	{
    		int result = player.GetInput();
    		if (result == 1)
    		{
    			Deal(1, player);
    			return 1; // is player's hand > 21?
    		}
    		if (result == 2) // Should probably be else if (result == 2)
    		{
    			return 2; //compare cards declare winner
    		}
    		if (result == 3) // Should probably be else if (result == 3)
    		{	
    			return 3; //player folded, remove player from game.
    		}
    		result = player.GetInput();
    	}
    Although I suppose it doesn't matter much since you're returning after the IF, but anyway.
    From what I can see, it might just be circular dependencies? A uses B and B uses A. That's the best I can say right now, anyway.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Circular dependencies eh? I think dealer uses human_player, which are both subclasses to player.. And then game uses dealer and player... I guess it could just use player, then that might eliminate some issues.... Any suggestions?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    First you'll have to find out if this is the case. Easiest way is to look for header includes header, like A.h includes B.h and B.h includes A.h. If you find that scenario, then you have circular depenencies.

    Then you might need one of these solutions. Either:
    Put forward declarations (like
    Code:
    class human_player;
    ).
    --OR--
    Put declaration of A.h into B.h or vice versa.
    --OR--
    Redesign your classes to avoid circular references.

    If your class members (not using pointers), then solution one won't work (because the compiler can't call the constructor to your class, nor destructor since it only knows the class exists, but doesn't have the declaration for it).
    Or you could just make that member a pointer and allocate using new in your constructor and delete it in the destructor.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    found it!!! bah my eyes are going bad or something..



    if {player.playerid == 0)



    has a { instead of a (.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Simple typo, eh?
    So the first compile error gone too, or is it still there?

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I fixed the other error by removing all my using namespace std; declarations in all my headers
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21