-
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;
}
-
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.
-
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.
-
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?
-
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.