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 I'm trying to split the game class up, and I've been looking online and stuff and I could be ...

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

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

    Okay I'm trying to split the game class up, and I've been looking online and stuff and I could be doing this horrible wrong, I'll show you guys the .h and .cpp file.

    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();
    
    
    private:
    
    	int blackjack(int result) const;
    	int get_hand_value(vector<Card> hand) const;
    
    	 int sum;
    	 Player player;
    	 Dealer dealer;
    	 Deck deck;
    	 text_menu tmenu;
    	 IO io;
    
    };
    #endif
    And this io the CPP file.
    Code:
    #include "Game.h"
    #include <iostream>
    
    Game::Game()
    {}
    
    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)
    {
    	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)
    {
    	sum = 0;
    	for (unsigned int loop = 0; loop < hand.size(); loop++)
    	{
    		sum += hand[loop].read_numeric_data();
    	}
    	return sum;
    }
    And idea why im getting an unexpected end of file error at the end of Game.cpp?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see it here, but the common reason for this error is that you've missed an "end" of some sort pf pair, e.g. braces, parenthesis, #if/#endif.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Unfortunately I cannot find any missing ends either..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Be aware of missing ones in "earlier header files" too - check if the same thing happens if you put #if 0/#endif about all the actual content in your .h or .c file. I've had that problem before: Some other header file contains an un-ended class/struct declaration...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    turns out a precompiled header was the problem, stdafx.h
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Shamino View Post
    turns out a precompiled header was the problem, stdafx.h
    Elysia, are you listening?

    Yes, that can cause problems too!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I have a few errors now, not quite sure what I'm doing wrong, web resources aren't helping much:

    Here are the errors:

    Code:
    ------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
    Compiling...
    Blackjack.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\io.h(53) : warning C4018: '<' : signed/unsigned mismatch
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\blackjack.cpp(9) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    Game.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\io.h(53) : warning C4018: '<' : signed/unsigned mismatch
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(6) : error C2533: 'Game::{ctor}' : constructors not allowed a return type
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(34) : error C2511: 'int Game::blackjack(int)' : overloaded member function not found in 'Game'
            c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.h(9) : see declaration of 'Game'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(60) : error C2511: 'int Game::get_hand_value(std::vector<_Ty>)' : overloaded member function not found in 'Game'
            with
            [
                _Ty=Card
            ]
            c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.h(9) : see declaration of 'Game'
    Generating Code...
    Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 3 error(s), 3 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Same code as before, only this time I have stdafx.h included at the top of Game.cpp
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I fixed the two errors involving the two functions, but I have the constructor error still, the two functions had an uneeded const declared in the header definition.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by matsp View Post
    Elysia, are you listening?

    Yes, that can cause problems too!

    --
    Mats
    Yes, everything can.
    But it's not the only thing that can make you confused.
    And besides, it's better to learn how to find and correct this error earlier than later, is it not?
    Like when I typed
    Code:
    int max = CArray<int>::Align((DWORD)max_);
    ...instead of...
    Code:
    int max = Stuff::CArray<int>::Align((DWORD)max_);
    The compiler just complained Align isn't part of CArray (probably referring to MFC's implementation).

    Blame it on the compiler
    But emmm... if the PCH is missing, then it usually complains
    Code:
    fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
    Is that what you got?
    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.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Any ideas about the constructors though? The error did mention stdafx.h

    The constructor is giving me that error i referred to earlier
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Shamino View Post
    Any ideas about the constructors though?
    I don't get such errors. Maybe you have an updated source? There's no problem in the code above.

    The error did mention stdafx.h
    Next time, you should probably mention the whole error, because it makes things easier

    Aside from that...
    Here are some problems I found:
    Code:
    int get_hand_value(vector<Card> hand) const;
    The function should probably take a reference, and a const one:
    Code:
    int get_hand_value(const vector<Card>& hand) const;
    The code also modifies the member variable sum, which is a no-no since it's const.
    Further, it takes an argument of the hand to calculate. Why? If it's a member function, then it should calculate from a hand stored inside the object. Otherwise you can make the function static.

    Code:
    int Game::blackjack(int result) const
    Missing const at end -> I added it.

    Code:
    int Game::get_hand_value(vector<int> hand) const
    Missing const at end -> I added it.
    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.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm still getting some errors, here is the updated code and errors:

    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) const;
    
    	 int sum;
    	 Player player;
    	 Dealer dealer;
    	 Deck deck;
    	 text_menu tmenu;
    	 IO io;
    
    };
    #endif
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include "Game.h"
    
    Game::Game();
    
    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)
    {
    	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)
    {
    	sum = 0;
    	for (unsigned int loop = 0; loop < hand.size(); loop++)
    	{
    		sum += hand[loop].read_numeric_data();
    	}
    	return sum;
    }
    Here are the errors:
    Code:
    ------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
    Compiling...
    stdafx.cpp
    Compiling...
    Blackjack.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\io.h(53) : warning C4018: '<' : signed/unsigned mismatch
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\blackjack.cpp(9) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    Game.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\io.h(53) : warning C4018: '<' : signed/unsigned mismatch
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(5) : error C2761: '{ctor}' : member function redeclaration not allowed
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(33) : error C2511: 'int Game::blackjack(int)' : overloaded member function not found in 'Game'
            c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.h(9) : see declaration of 'Game'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.cpp(59) : error C2511: 'int Game::get_hand_value(std::vector<_Ty> &)' : overloaded member function not found in 'Game'
            with
            [
                _Ty=Card
            ]
            c:\documents and settings\jcoleman\desktop\blackjack\blackjack\game.h(9) : see declaration of 'Game'
    Generating Code...
    Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 3 error(s), 3 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Any ideas?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    You must define your constructor in the .cpp file - you're only putting an empty stub which is a declaration and not allowed.
    And you haven't addressed what I informed you of above.
    And the unsigned/signed warning, make both types signed or unsigned.
    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.

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I fixed all my errors except for these:
    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(59) : error C2166: l-value specifies const object
    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
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\blackjack.cpp(9) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    Generating Code...
    Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 3 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Something about converting cards to data and players to &players....

    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) const;
    
    	 int sum;
    	 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) const
    {
    	sum = 0;
    	for (unsigned int loop = 0; loop < hand.size(); loop++)
    	{
    		sum += hand[loop].read_numeric_data();
    	}
    	return sum;
    }
    It's crazy how many extra errors I'm producing by splitting up this file. I still am not sure what to do about sum and the const conflict.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Is any other function using sum? No? Then declare it locally within the member function. If other member function are using it, then remove const from the function.
    A function which is const cannot modify any class member data. That's what the compiler is complaining about.
    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 1 of 2 12 LastLast
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