Unexpected end of file, splitting headers into .h/.cpp

This is a discussion on Unexpected end of file, splitting headers into .h/.cpp within the C++ Programming forums, part of the General Programming Boards category; Okay cool, now those 3 have converted into 5 other errors, all strange and never before seen by my eyes. ...

  1. #16
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Okay cool, now those 3 have converted into 5 other errors, all strange and never before seen by my eyes.

    Code:
    ------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
    Compiling...
    Game.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(35) : error C2662: 'Player::draw' : cannot convert 'this' pointer from 'const Player' to 'Player &'
            Conversion loses qualifiers
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(36) : error C2662: 'Game::get_hand_value' : cannot convert 'this' pointer from 'const Game' to 'Game &'
            Conversion loses qualifiers
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(44) : error C2662: 'Game::get_hand_value' : cannot convert 'this' pointer from 'const Game' to 'Game &'
            Conversion loses qualifiers
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(44) : error C2662: 'Game::get_hand_value' : cannot convert 'this' pointer from 'const Game' to 'Game &'
            Conversion loses qualifiers
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(62) : error C2662: 'Data::read_numeric_data' : cannot convert 'this' pointer from 'const Card' to 'Data &'
            Conversion loses qualifiers
    Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 5 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I think my code is how you were saying before.
    Code:
    #ifndef GAME_H
    #define GAME_H
    #include "Dealer.h"
    #include <vector>
    #include "IO.H"
    #include "Player.h"
    
    class Game
    {
    public:
    
    	Game(){}
    
    
    	void play_game(void);
    
    
    private:
    
    	int blackjack(int result) const;
    	int get_hand_value(const std::vector<Card> & hand);
    
    	 Player player;
    	 Dealer dealer;
    	 Deck deck;
    	 text_menu tmenu;
    	 IO io;
    
    };
    #endif
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include "Game.h"
    
    
    void Game::play_game()
    {
    	//shuffle the deck first
    	dealer.shuffle_deck(deck);
    	//draw cards for players
    	dealer.draw(deck, 1);
    	player.draw(deck, 2);
    	//initialize the gameloop
    	int gameloop = 1;
    	//initialize output
    	io.init_output(tmenu);
    	//start gameloop
    	while(gameloop != 0)
    	{
    		//update
    		io.update_output(2, dealer.hand->read_cards());
    		io.update_output(6, player.hand->read_cards());
    		//display
    		io.display_output();
    		gameloop = blackjack(io.get_input()); 
    		//clear
    		io.clear_screen();
    	}
    }
    
    int Game::blackjack(int result) const 
    {
    	if (result == 1)
    	{
    		player.draw(deck, 1);
    		if (get_hand_value(player.hand->card_list) > 21)
    		{
    			return 0;
    		}
    		else { return 1; }
    	}
    	else if (result == 2)
    	{
    		if(get_hand_value(player.hand->card_list ) > get_hand_value(dealer.hand->card_list))  
    		{
    			return 0;
    		}
    		else { return 0; }
    	}
    	else if (result == 3)
    	{	
    		return 0;
    	}
    	return 1;
    }
    
    int Game::get_hand_value(const vector<Card> & hand)
    {
    	int sum = 0;
    	for (unsigned int loop = 0; loop < hand.size(); loop++)
    	{
    		sum += hand[loop].read_numeric_data();
    	}
    	return sum;
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #17
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I've fixed everything, here's the code.

    Code:
    #ifndef GAME_H
    #define GAME_H
    #include "Dealer.h"
    #include <vector>
    #include "IO.H"
    #include "Player.h"
    
    class Game
    {
    public:
    
    	Game(){}
    
    
    	void play_game(void);
    
    
    private:
    
    	int blackjack(int result);
    	int get_hand_value(std::vector<Card> & hand);
    
    	 Player player;
    	 Dealer dealer;
    	 Deck deck;
    	 text_menu tmenu;
    	 IO io;
    
    };
    #endif
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include "Game.h"
    
    
    void Game::play_game()
    {
    	//shuffle the deck first
    	deck.shuffle_deck();
    	//draw cards for players
    	dealer.draw(deck, 1);
    	player.draw(deck, 2);
    	//initialize the gameloop
    	int gameloop = 1;
    	//initialize output
    	io.init_output(tmenu);
    	//start gameloop
    	while(gameloop != 0)
    	{
    		//update
    		io.update_output(2, dealer.hand->read_cards());
    		io.update_output(6, player.hand->read_cards());
    		//display
    		io.display_output();
    		gameloop = blackjack(io.get_input()); 
    		//clear
    		io.clear_screen();
    	}
    }
    
    int Game::blackjack(int result) 
    {
    	if (result == 1)
    	{
    		player.draw(deck, 1);
    		if (get_hand_value(player.hand->card_list) > 21)
    		{
    			return 0;
    		}
    		else { return 1; }
    	}
    	else if (result == 2)
    	{
    		if(get_hand_value(player.hand->card_list ) > get_hand_value(dealer.hand->card_list))  
    		{
    			return 0;
    		}
    		else { return 0; }
    	}
    	else if (result == 3)
    	{	
    		return 0;
    	}
    	return 1;
    }
    
    int Game::get_hand_value(vector<Card> & hand)
    {
    	int sum = 0;
    	for (unsigned int loop = 0; loop < hand.size(); loop++)
    	{
    		sum += hand[loop].read_numeric_data();
    	}
    	return sum;
    }
    I've started on the hand class now.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    get_hand_value can be const because it doesn't change class data.
    Argument hand should also be const because it doesn't change.
    Depending on player.draw, blackjack may or may not be const. Take that into consideration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I get an error when I try to make the get_hand_value function have a constant &hand parameter.

    Code:
    ------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
    Compiling...
    Game.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(62) : error C2662: 'Data::read_numeric_data' : cannot convert 'this' pointer from 'const Card' to 'Data &'
            Conversion loses qualifiers
    Generating Code...
    Compiling...
    Blackjack.cpp
    Generating Code...
    Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    It looks like this:
    Declaration
    Code:
    int get_hand_value(const std::vector<Card> & hand);
    Implementation
    Code:
    int Game::get_hand_value(const vector<Card> & hand)
    {
    	int sum = 0;
    	for (unsigned int loop = 0; loop < hand.size(); loop++)
    	{
    		sum += hand[loop].read_numeric_data();
    	}
    	return sum;
    }
    I know that a Card is derived from Data, since Cards have data that can be automatically generated in a lookup tables, (like the string of the card or the face value of the card).

    What is the problem with my syntax?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The compiler says this line is the source of the error:
    Code:
    		sum += hand[loop].read_numeric_data();
    So if the function is just supposed to read some numeric data, it should also be const.
    You can't call a non-const function from a const function - that's the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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