Avatar billede jih Nybegynder
21. juli 2008 - 12:14 Der er 20 kommentarer og
1 løsning

fejlfinding: 13+ fejl i 498 linjer kode

Hej..

Jeg har skrevet en meget stor kode ind på Visual Studio 2008 Professional (ifølge arne_v i http://www.eksperten.dk/spm/838900 skulle det virke på en eller anden måde).. jeg er sikker på at den virkede i VS98, dog kan det være der er skrevet fejl et par steder.. er der nogen der vil/kan hjælpe mig over email/msn?
jeg har koden liggende i et tekstdokument, og fejlene har jeg taget screenshot af, så jeg kan sende filerne over email/msn, hvis nogen er villig til at hjælpe? :-)

På forhånd tak..

// jih
Avatar billede kenneth_gorking Nybegynder
21. juli 2008 - 22:04 #1
Hvad er der for noget kode? Hvis du prøver at bruge MFC f.eks, så vil det ikke virke. De skiftede det helt helt ud for nyligt, blandt andet på grund af MFC's mange brud på C++ standarden.

Det ville være nemmere at starte forfra med et rent MFC projekt, og så inkorporere dine ændringer deri.
Avatar billede arne_v Ekspert
22. juli 2008 - 04:12 #2
Der er ihvertfald umulig at hjælpe uden at se noget kode og nogle fejl beskeder.

VC++ 2008 skulle stadig indeholde MFC.

Men der er mange options man kan sætte og hvis options ikke matcher koden, så
giver det fejl.
Avatar billede arne_v Ekspert
22. juli 2008 - 04:13 #3
Kan du ikke bare poste kode og fejl ?

Det fylder lidt, men skidt pyt med det.
Avatar billede jih Nybegynder
22. juli 2008 - 11:11 #4
javel så.. her er koden:

--
// Blackjack.cpp : Defines the entry point for the console application.
// Description: Plays a simple version of the casino game of blackjack; for 1 - 7 players.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

class Card
{
public:
    enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
    enum suit {CLUBS, DIAMOND, HEARTS, SPADES};

    // overloading << operator so can send Card object to standard output
    friend ostream& operator<<(ostream& os, const Card& aCard);

    Card(rank r = ACE, suit s = SPADES, bool ifu = true);

    // returns the value of a card, 1 - 11
    int GetValue() const;

    // flips a card; if face up, becomes face down and vice versa
    void Flip();

private:
    rank m_Rank;
    suit m_Suit;
    bool m_IsFaceUp;
};

Card::Card(rank r, suit s, bool ifu): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu)
{}

int Card::GetValue() const
{
    // if a card is face down, its value is 0
    int value = 0;
    if (m_IsFaceUp)
    {
        // value is number showing on card
        value = m_Rank;
        // value is 10 for face cards
        if (value > 10)
            value = 10;
    }
    return value;
}

void Card::Flip()
{
    m_IsFaceUp = !(m_IsFaceUp);
}

class Hand
{
public:
    Hand();

    virtual ~Hand();

    // adds a card to the hand
    void Add(Card* pCard);

    // clears hand of all cards
    void Clear();

    // gets hand total value, intelligently treats aces as 1 or 11
    int GetTotal() const;

protected:
    vector<Card*> m_Cards;
};

Hand::Hand()
{
    m_Cards.reserve(7);
}

Hand::~Hand() // don't use the keyword virtual outside of class definition
{
    Clear();
}

void Hand::Add(Card* pCard)
{
    m_Cards.push_back(pCard);
}

void Hand::Clear()
{
    // iterate through vector, freeing all memory on the heap
    vector<Card*>::iterator iter = m_Cards.begin();
    for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
    {
        delete *iter;
        *iter = 0;
    }
    // clear vector of pointers
    m_Cards.clear();
}

int Hand::GetTotal() const
{
    // if no cards in hand, return 0
    if (m_Cards.empty())
        return 0;

    // if a first card has value of 0, then card is face down; return 0
    if (m_Cards[0]->GetValue() == 0)
        return 0;

    // add up card values, treat each ace as 1
    int total = 0;
    vector<Card*>::const_iterator iter;
    for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
        total += (*iter)->GetValue();

    // determine if hand contains an ace
    bool containsAce = false;
    for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
        if ((*iter)->GetValue() == Card::ACE)
            containsAce = true;

    // if hand contains ace and total is low enough, treat ace as 11
    if (containsAce && total <= 11)
        // add only 10 since we've already added 1 for the ace
        total += 10;

    return total;
}

class GenericPlayer : public Hand
{
    friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);

public:
    GenericPlayer(const string& name = "");

    virtual ~GenericPlayer();

    // indicates whether or not generic player wants to keep hitting
    virtual bool IsHitting() const = 0;

    // returns whether generic player has busted - has a total greater than 21
    bool IsBusted() const;

    // announced that the generic player busts
    void Bust() const;

protected:
    string m_Name;
};

GenericPlayer::GenericPlayer(const string& name): m_Name(name)
{}

GenericPlayer::~GenericPlayer()
{}

bool GenericPlayer::IsBusted() const
{
    return (GetTotal() > 21);
}

void GenericPlayer::Bust() const
{
    cout << m_Name << " busts.\n";
}

class Player : public GenericPlayer
{
public:
    Player(const string& name = "");

    virtual ~Player();

    // returns whether or not the player wants another hit
    virtual bool IsHitting() const;

    // announces that the player wins
    void Win() const;

    // announces that the player loses
    void Lose() const;

    // announces that the player pushes
    void Push() const;
};

Player::Player(const string& name): GenericPlayer(name)
{}

Player::~Player()
{}

bool Player::IsHitting() const
{
    cout << m_Name << ", do you want a hit? (Y/N): ";
    char response;
    cin >> response;
    return (response == 'y' || response == 'Y');
}

void Player::Win() const
{
    cout << m_Name << " wins.\n";
}

void Player::Lose() const
{
    cout << m_Name << " loses.\n";
}

void Player::Push() const
{
    cout << m_Name << " pushes.\n";
}

class House : public GenericPlayer
{
public:
    House(const string& name = "House");

    virtual ~House();

    // indicates whether house is hitting - will always hit on 16 or less
    virtual bool IsHitting() const;

    // flips over first card
    void FlipFirstCard();
};

House::House(const string& name): GenericPlayer(name)
{}

House::~House()
{}

bool House::IsHitting() const
{
    return (GetTotal() <= 16);
}

void House::FlipFirstCard()
{
    if (!(m_Cards.empty()))
        m_Cards[0]->Flip();
    else cout << "No card to flip!\n";
}

class Deck : public Hand
{
public:
    Deck();

    virtual ~Deck();

    // create a standard deck of 52 cards
    void Populate();

    // shuffle cards
    void Shuffle();

    // deal one card to a hand
    void Deal(Hand& aHand);

    // give additional cards to a generic player
    void AdditionalCards(GenericPlayer& aGenericPlayer);
};

Deck::Deck()
{
    m_Cards.reserve(52);
    Populate();
}

Deck::~Deck()
{}

void Deck::Populate()
{
    Clear();
    // create standard deck
    for (int s = Card::CLUBS; s <= Card::SPADES; ++s)
        for (int r = Card::ACE; r <= Card::KING; ++r)
            Add(new Card(static_cast<Card::rank>(r), static_cast<Card::suit>(s)));
}

void Deck::Shuffle()
{
    random_shuffle(m_Cards.begin(), m_Cards.end());
}

void Deck::Deal(Hand& aHand)
{
    if (!m_Cards.empty())
    {
        aHand.Add(m_Cards.back());
        m_Cards.pop_back();
    }
    else
    {
        cout << "Out of cards. Unable to deal.";
    }
}

void Deck::AdditionalCards(GenericPlayer& aGenericPlayer)
{
    cout << endl;
    // continue to deal a card as long as generic player isn't busted and
    // wants another hit
    while (!(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting())
    {
        Deal(aGenericPlayer);
        cout << aGenericPlayer << endl;

        if (aGenericPlayer.IsBusted())
            aGenericPlayer.Bust();
    }
}

class Game
{
public:
    Game(const vector<string>& names);

    ~Game();

    // plays the game of blackjack
    void Play();

private:
    Deck m_Deck;
    House m_House;
    vector<Player> m_Players;
};

Game::Game(const vector<string>& names)
{
    // create a vector of players from a vector of names
    vector<string>::const_iterator pName;
    for (pName = names.begin(); pName != names.end(); ++pName)
        m_Players.push_back(Player(*pName));

    srand(time(0));            // seed the random number generator
    m_Deck.Populate();
    m_Deck.Shuffle();
}

Game::~Game()
{}

void Game::Play()
{
    // deal initial 2 cards to everyone
    vector<Player>::iterator pPlayer;
    for (int i = 0; i < 2; ++i)
    {
        for (pPlayer = m_Player.begin(); pPlayer != m_Player.end(); ++pPlayer)
            m_Deck.Deal(*pPlayer);
        m_Deck.Deal(m_House);
    }

    // hide fouse's first card
    m_House.FlipFirstCard();

    // display everyone's hand
    for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
        cout << *pPlayer << endl;
    cout << m_House << endl;

    // deal additional cards to players
    for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
        m_Deck.AdditionalCards(*pPlayer);

    // reveal house's first card
    m_House.FlipFirstCard();
    cout << endl << m_House;

    // deal additional cards to house
    m_Deck.AdditionalCards(m_House);

    if (m_House.IsBusted())
    {
        // everyone still playing wins
        for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
            if (!(pPlayer->IsBusted()))
                pPlayer->Win();
    }
    else
    {
        // compare each player still paying to house
        for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
            if (!(pPlayer->IsBusted()))
            {
                if (pPlayer->GetTotal() > m_House.GetTotal())
                    pPlayer.Win();
                else if (pPlayer->GetTotal() < m_House.GetTotal())
                    pPlayer.Lose();
                else
                    pPlayer.Push();
            }
    }

    // remove everyone's cards
    for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
        pPlayer->Clear();
    m_House->Clear();
}

// function prototypes
ostream& operator<<(ostream& os, const Card& aCard);
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);

int main()
{
    cout << "\t\tWelcome to Blackjack!\n\n";

    int numPlayer = 0;
    while (numPlayers < 1 && numPlayers > 7)
    {
        cout << "How many players? (1 - 7): ";
        cin >> numPlayers;
    }

    vector<string> names;
    string name;
    for (int i = 0; i < numPlayers; ++i)
    {
        cout << "Enter player name: ";
        cin >> name;
        names.push_back(name);
    }
    cout << endl;

    // the game loop
    Game aGame(names);
    char again = 'y';
    while (again != 'n' && again != 'N')
    {
        aGame.Play();
        cout << "\nDo you want to play again? (Y/N): ";
        cin >> again;
    }

    system("pause");
    return (0);
}

// overloads << operator so Card object can be sent to cout
ostream& operator<<(ostream& os, const Card& aCard)
{
    const string RANKS[] = {"0", "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    const string SUITS[] = {"c", "d", "h", "s"};

    if (aCard.m_IsFaceUp)
        os << RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit];
    else
        os << "XX";

    return os;
}

// overloads << operator so GenericPlayer object can be sent to cout
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer)
{
    os << aGenericPlayer.m_Name << ":\t";

    vector<Card*>::const_iterator pCard;
    if (!aGenericPlayer.m_Cards.empty())
    {
        for (pCard = aGenericPlayer.m_Cards.begin(); pCard != aGenericPlayer.end(); ++pCard)
            os << *(*pCard) << "\t";

        if (aGenericPlayer.GetTotal() != 0)
            cout << "(" << aGenericPlayer.GetTotal() << ")";
    }
    else
    {
        os << "<empty>";
    }
    return os;
}
--

fejlene har jeg taget screenshot af og sendt til min email så jeg kan poste dem mens jeg er på arbejde, så jeg kan ikke helt skrive dem her.. - imageshack virker ikke på denne computer
Avatar billede jih Nybegynder
22. juli 2008 - 11:19 #5
nu virkede det åbenbart alligevel:-)

her er link til fejlene: http://img112.imageshack.us/img112/5840/blackjackerrorstv4.jpg
Avatar billede arne_v Ekspert
22. juli 2008 - 23:51 #6
2 steder skal m_Player. rettes til m_Players.
Avatar billede arne_v Ekspert
22. juli 2008 - 23:53 #7
int numPlayer = 0;

skal rettes til

int numPlayers = 0;
Avatar billede arne_v Ekspert
22. juli 2008 - 23:54 #8
et sted skal

aGenericPlayer.end()

rettes til

aGenericPlayer.m_Cards.end()
Avatar billede arne_v Ekspert
22. juli 2008 - 23:55 #9
et sted skal

m_House->Clear();

rettes til

m_House.Clear();
Avatar billede arne_v Ekspert
22. juli 2008 - 23:56 #10
3 steder skal

pPlayer.

rettes til

pPlayer->
Avatar billede arne_v Ekspert
22. juli 2008 - 23:56 #11
// Blackjack.cpp : Defines the entry point for the console application.
// Description: Plays a simple version of the casino game of blackjack; for 1 - 7 players.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

class Card
{
public:
    enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
    enum suit {CLUBS, DIAMOND, HEARTS, SPADES};

    // overloading << operator so can send Card object to standard output
    friend ostream& operator<<(ostream& os, const Card& aCard);

    Card(rank r = ACE, suit s = SPADES, bool ifu = true);

    // returns the value of a card, 1 - 11
    int GetValue() const;

    // flips a card; if face up, becomes face down and vice versa
    void Flip();

private:
    rank m_Rank;
    suit m_Suit;
    bool m_IsFaceUp;
};

Card::Card(rank r, suit s, bool ifu): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu)
{}

int Card::GetValue() const
{
    // if a card is face down, its value is 0
    int value = 0;
    if (m_IsFaceUp)
    {
        // value is number showing on card
        value = m_Rank;
        // value is 10 for face cards
        if (value > 10)
            value = 10;
    }
    return value;
}

void Card::Flip()
{
    m_IsFaceUp = !(m_IsFaceUp);
}

class Hand
{
public:
    Hand();

    virtual ~Hand();

    // adds a card to the hand
    void Add(Card* pCard);

    // clears hand of all cards
    void Clear();

    // gets hand total value, intelligently treats aces as 1 or 11
    int GetTotal() const;

protected:
    vector<Card*> m_Cards;
};

Hand::Hand()
{
    m_Cards.reserve(7);
}

Hand::~Hand() // don't use the keyword virtual outside of class definition
{
    Clear();
}

void Hand::Add(Card* pCard)
{
    m_Cards.push_back(pCard);
}

void Hand::Clear()
{
    // iterate through vector, freeing all memory on the heap
    vector<Card*>::iterator iter = m_Cards.begin();
    for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
    {
        delete *iter;
        *iter = 0;
    }
    // clear vector of pointers
    m_Cards.clear();
}

int Hand::GetTotal() const
{
    // if no cards in hand, return 0
    if (m_Cards.empty())
        return 0;

    // if a first card has value of 0, then card is face down; return 0
    if (m_Cards[0]->GetValue() == 0)
        return 0;

    // add up card values, treat each ace as 1
    int total = 0;
    vector<Card*>::const_iterator iter;
    for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
        total += (*iter)->GetValue();

    // determine if hand contains an ace
    bool containsAce = false;
    for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
        if ((*iter)->GetValue() == Card::ACE)
            containsAce = true;

    // if hand contains ace and total is low enough, treat ace as 11
    if (containsAce && total <= 11)
        // add only 10 since we've already added 1 for the ace
        total += 10;

    return total;
}

class GenericPlayer : public Hand
{
    friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);

public:
    GenericPlayer(const string& name = "");

    virtual ~GenericPlayer();

    // indicates whether or not generic player wants to keep hitting
    virtual bool IsHitting() const = 0;

    // returns whether generic player has busted - has a total greater than 21
    bool IsBusted() const;

    // announced that the generic player busts
    void Bust() const;

protected:
    string m_Name;
};

GenericPlayer::GenericPlayer(const string& name): m_Name(name)
{}

GenericPlayer::~GenericPlayer()
{}

bool GenericPlayer::IsBusted() const
{
    return (GetTotal() > 21);
}

void GenericPlayer::Bust() const
{
    cout << m_Name << " busts.\n";
}

class Player : public GenericPlayer
{
public:
    Player(const string& name = "");

    virtual ~Player();

    // returns whether or not the player wants another hit
    virtual bool IsHitting() const;

    // announces that the player wins
    void Win() const;

    // announces that the player loses
    void Lose() const;

    // announces that the player pushes
    void Push() const;
};

Player::Player(const string& name): GenericPlayer(name)
{}

Player::~Player()
{}

bool Player::IsHitting() const
{
    cout << m_Name << ", do you want a hit? (Y/N): ";
    char response;
    cin >> response;
    return (response == 'y' || response == 'Y');
}

void Player::Win() const
{
    cout << m_Name << " wins.\n";
}

void Player::Lose() const
{
    cout << m_Name << " loses.\n";
}

void Player::Push() const
{
    cout << m_Name << " pushes.\n";
}

class House : public GenericPlayer
{
public:
    House(const string& name = "House");

    virtual ~House();

    // indicates whether house is hitting - will always hit on 16 or less
    virtual bool IsHitting() const;

    // flips over first card
    void FlipFirstCard();
};

House::House(const string& name): GenericPlayer(name)
{}

House::~House()
{}

bool House::IsHitting() const
{
    return (GetTotal() <= 16);
}

void House::FlipFirstCard()
{
    if (!(m_Cards.empty()))
        m_Cards[0]->Flip();
    else cout << "No card to flip!\n";
}

class Deck : public Hand
{
public:
    Deck();

    virtual ~Deck();

    // create a standard deck of 52 cards
    void Populate();

    // shuffle cards
    void Shuffle();

    // deal one card to a hand
    void Deal(Hand& aHand);

    // give additional cards to a generic player
    void AdditionalCards(GenericPlayer& aGenericPlayer);
};

Deck::Deck()
{
    m_Cards.reserve(52);
    Populate();
}

Deck::~Deck()
{}

void Deck::Populate()
{
    Clear();
    // create standard deck
    for (int s = Card::CLUBS; s <= Card::SPADES; ++s)
        for (int r = Card::ACE; r <= Card::KING; ++r)
            Add(new Card(static_cast<Card::rank>(r), static_cast<Card::suit>(s)));
}

void Deck::Shuffle()
{
    random_shuffle(m_Cards.begin(), m_Cards.end());
}

void Deck::Deal(Hand& aHand)
{
    if (!m_Cards.empty())
    {
        aHand.Add(m_Cards.back());
        m_Cards.pop_back();
    }
    else
    {
        cout << "Out of cards. Unable to deal.";
    }
}

void Deck::AdditionalCards(GenericPlayer& aGenericPlayer)
{
    cout << endl;
    // continue to deal a card as long as generic player isn't busted and
    // wants another hit
    while (!(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting())
    {
        Deal(aGenericPlayer);
        cout << aGenericPlayer << endl;

        if (aGenericPlayer.IsBusted())
            aGenericPlayer.Bust();
    }
}

class Game
{
public:
    Game(const vector<string>& names);

    ~Game();

    // plays the game of blackjack
    void Play();

private:
    Deck m_Deck;
    House m_House;
    vector<Player> m_Players;
};

Game::Game(const vector<string>& names)
{
    // create a vector of players from a vector of names
    vector<string>::const_iterator pName;
    for (pName = names.begin(); pName != names.end(); ++pName)
        m_Players.push_back(Player(*pName));

    srand(time(0));            // seed the random number generator
    m_Deck.Populate();
    m_Deck.Shuffle();
}

Game::~Game()
{}

void Game::Play()
{
    // deal initial 2 cards to everyone
    vector<Player>::iterator pPlayer;
    for (int i = 0; i < 2; ++i)
    {
        for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
            m_Deck.Deal(*pPlayer);
        m_Deck.Deal(m_House);
    }

    // hide fouse's first card
    m_House.FlipFirstCard();

    // display everyone's hand
    for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
        cout << *pPlayer << endl;
    cout << m_House << endl;

    // deal additional cards to players
    for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
        m_Deck.AdditionalCards(*pPlayer);

    // reveal house's first card
    m_House.FlipFirstCard();
    cout << endl << m_House;

    // deal additional cards to house
    m_Deck.AdditionalCards(m_House);

    if (m_House.IsBusted())
    {
        // everyone still playing wins
        for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
            if (!(pPlayer->IsBusted()))
                pPlayer->Win();
    }
    else
    {
        // compare each player still paying to house
        for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
            if (!(pPlayer->IsBusted()))
            {
                if (pPlayer->GetTotal() > m_House.GetTotal())
                    pPlayer->Win();
                else if (pPlayer->GetTotal() < m_House.GetTotal())
                    pPlayer->Lose();
                else
                    pPlayer->Push();
            }
    }

    // remove everyone's cards
    for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
        pPlayer->Clear();
    m_House.Clear();
}

// function prototypes
ostream& operator<<(ostream& os, const Card& aCard);
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);

int main()
{
    cout << "\t\tWelcome to Blackjack!\n\n";

    int numPlayers = 0;
    while (numPlayers < 1 && numPlayers > 7)
    {
        cout << "How many players? (1 - 7): ";
        cin >> numPlayers;
    }

    vector<string> names;
    string name;
    for (int i = 0; i < numPlayers; ++i)
    {
        cout << "Enter player name: ";
        cin >> name;
        names.push_back(name);
    }
    cout << endl;

    // the game loop
    Game aGame(names);
    char again = 'y';
    while (again != 'n' && again != 'N')
    {
        aGame.Play();
        cout << "\nDo you want to play again? (Y/N): ";
        cin >> again;
    }

    system("pause");
    return (0);
}

// overloads << operator so Card object can be sent to cout
ostream& operator<<(ostream& os, const Card& aCard)
{
    const string RANKS[] = {"0", "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    const string SUITS[] = {"c", "d", "h", "s"};

    if (aCard.m_IsFaceUp)
        os << RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit];
    else
        os << "XX";

    return os;
}

// overloads << operator so GenericPlayer object can be sent to cout
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer)
{
    os << aGenericPlayer.m_Name << ":\t";

    vector<Card*>::const_iterator pCard;
    if (!aGenericPlayer.m_Cards.empty())
    {
        for (pCard = aGenericPlayer.m_Cards.begin(); pCard != aGenericPlayer.m_Cards.end(); ++pCard)
            os << *(*pCard) << "\t";

        if (aGenericPlayer.GetTotal() != 0)
            cout << "(" << aGenericPlayer.GetTotal() << ")";
    }
    else
    {
        os << "<empty>";
    }
    return os;
}
Avatar billede jih Nybegynder
23. juli 2008 - 10:33 #12
jeg afprøver koden når jeg kommer hjem
Avatar billede jih Nybegynder
23. juli 2008 - 13:56 #13
jeg har afinstalleret visual studio 2008 og installeret vs6 - fandt informationer på nettet om hvordan jeg kunne installere vs6, så koden på vs6 giver disse fejl:

--------------------Configuration: Blackjack - Win32 Debug--------------------
Compiling...
Blackjack.cpp
c:\windows\system32\blackjack.cpp(353) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,s
td::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\windows\system32\blackjack.cpp(353) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::ch
ar_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\windows\system32\blackjack.cpp(461) : error C2248: 'm_IsFaceUp' : cannot access private member declared in class 'Card'
        c:\windows\system32\blackjack.cpp(33) : see declaration of 'm_IsFaceUp'
c:\windows\system32\blackjack.cpp(462) : error C2248: 'm_Rank' : cannot access private member declared in class 'Card'
        c:\windows\system32\blackjack.cpp(31) : see declaration of 'm_Rank'
c:\windows\system32\blackjack.cpp(462) : error C2248: 'm_Suit' : cannot access private member declared in class 'Card'
        c:\windows\system32\blackjack.cpp(32) : see declaration of 'm_Suit'
c:\windows\system32\blackjack.cpp(472) : error C2248: 'm_Name' : cannot access protected member declared in class 'GenericPlayer'
        c:\windows\system32\blackjack.cpp(156) : see declaration of 'm_Name'
c:\windows\system32\blackjack.cpp(475) : error C2248: 'm_Cards' : cannot access protected member declared in class 'Hand'
        c:\windows\system32\blackjack.cpp(76) : see declaration of 'm_Cards'
c:\windows\system32\blackjack.cpp(477) : error C2248: 'm_Cards' : cannot access protected member declared in class 'Hand'
        c:\windows\system32\blackjack.cpp(76) : see declaration of 'm_Cards'
c:\windows\system32\blackjack.cpp(477) : error C2248: 'm_Cards' : cannot access protected member declared in class 'Hand'
        c:\windows\system32\blackjack.cpp(76) : see declaration of 'm_Cards'
c:\windows\system32\blackjack.cpp(478) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

Blackjack.obj - 8 error(s), 2 warning(s)
Avatar billede jih Nybegynder
23. juli 2008 - 15:10 #14
det skal nok lige nævnes at jeg havde lidt travlt da jeg installerede vs6, så jeg fik ikke installeret SP6.. det får jeg så forhåbentligt tid til at gøre senere i dag, når jeg har fri.. ved ikke om det er relevant eller ej, tænkte bare hvis der var vigtige ændringer i syntax o.lign..
Avatar billede jih Nybegynder
23. juli 2008 - 17:59 #15
nu har jeg installeret SP6, og får ingen fejl.. får dog 1 fejl når jeg prøver at builde .exe:

--------------------Configuration: Blackjack - Win32 Debug--------------------
Linking...
LINK : fatal error LNK1104: cannot open file "Debug/Blackjack.exe"
Error executing link.exe.

Blackjack.exe - 1 error(s), 0 warning(s)

hvad kan det skyldes ?
Avatar billede jih Nybegynder
23. juli 2008 - 18:03 #16
det var så fordi jeg ikke brugte "Kør som administrator".. men nu får jeg et problem når jeg spiller .. jeg kan ikke gøre noget.. uanset hvad jeg gør, så kommer computeren til at spille alene hele tiden..

http://img65.imageshack.us/img65/4099/blackjackyv3.jpg

jeg kan kun vælge "spil igen" og "afslut".. idéer?
Avatar billede arne_v Ekspert
24. juli 2008 - 02:24 #17
Måske

while (numPlayers < 1 && numPlayers > 7)

skal være

while (numPlayers < 1 || numPlayers > 7)
Avatar billede jih Nybegynder
24. juli 2008 - 10:03 #18
ah lol.. jeg har byttet om på operatørerne.. det skal så selvfølgelig være

while (numPlayers >= 1 && numPlayers <= 7)

eller... mener jeg da?

eller måske (numPlayers > 0 && numPlayers < 8) ?

ville det ikke være korrekt for den øverste kommentar?

// Blackjack.cpp : Defines the entry point for the console application.
// Description: Plays a simple version of the casino game of blackjack; for 1 - 7 players.
Avatar billede jih Nybegynder
24. juli 2008 - 10:52 #19
nå nej.. det er jo rigtigt som du siger..

glemte hvad for et loop det var - at den tjekker om der er rigtigt indtastet, og ikke om spillet er igang..

prøver while (numPlayers < 1 || numPlayers > 7) når jeg kommer hjem..

jeg har været rimelig træt når jeg skrev den kode.. + jeg skrev den af fra en bog ud i et, så der er diverse småfejl i.. ;-)
Avatar billede jih Nybegynder
24. juli 2008 - 13:42 #20
super, tak for hjælpen! funker præcis som det skal.. :-) smid et svar og du får dine points.. :-)
Avatar billede arne_v Ekspert
24. juli 2008 - 14:38 #21
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester