这是一个 flutter 克游戏,我在PokerTable.h中定义了类PokerTable

#include <iostream>
using namespace std;
class PokerTable
{
private:
 int numPlayers;
 int numPlaying;
 int dealerPos;
 int bigBlind;
 int potSize;
 int betSize;
 bool flop;
 bool turn;
 bool river;

 public:
 //constructors
 PokerTable();
 PokerTable(int,int,int,int,int,bool,bool,bool);

 //getters
 int getNumPlayers(){return numPlayers;};
 int getDealerPos(){return dealerPos;};
 int getBigBlind(){return bigBlind;};
 int getNumPlaying(){return numPlaying;};
 int getPotSize(){return potSize;};
 int getBetSize(){return betSize;};
 bool getFlop(){return flop;};
 bool getTurn(){return turn;};
 bool getRiver(){return river;};
 //void buttonShow(int);


 //setters
 void setBetSize(int inBetSize){betSize = inBetSize;};
 void setBigBlind(int inBigBlind){bigBlind = inBigBlind;};
 void setNumPlaying(int inNumPlaying){numPlaying = inNumPlaying;};
 void setPotSize(int inPotSize){potSize = inPotSize;};
 void setFlop(bool inFlop){flop = inFlop;};
 void setTurn(bool inTurn){turn = inTurn;};
 void setRiver(bool inRiver){river = inRiver;};
 void setNumPlayers(int inPlayers){numPlayers = inPlayers;};
 void setDealerPos(int inDealerPos){dealerPos = inDealerPos;};
};
PokerTable::PokerTable()
{
 numPlayers = 9;
 numPlaying = 9;
 dealerPos = 1;
 bigBlind = 20;
 flop = false;
 turn = false;
 river = false;
}
PokerTable::PokerTable(int playerNum, int playingCount, int posDealer, int blindBig,int inPotSize, bool inFlop,bool inTurn,bool inRiver)
{
 numPlayers = playerNum;
 numPlaying = playingCount;
 dealerPos = posDealer;
 potSize = inPotSize;
 bigBlind = blindBig;
 flop = inFlop;
 turn = inTurn;
 river = inRiver;
}

在我的监视列表中,pokerTable.numPlayers的随机值最大为400万,我什至不执行下一行代码。
PokerTable aPokerTable(9,9,1,20,30,false,false,false);

之后是我的监视列表中的pokerTable:
-       aPokerTable { numPlayers=2990892 numPlaying=9 dealerPos=9 ...}  PokerTable
        betSize 30  int
        bigBlind    1   int
        dealerPos   9   int
        flop    false   bool
        numPlayers  2990892 int
        numPlaying  9   int
        potSize 20  int
        river   false   bool
        turn    false   bool

谁能告诉我为什么所有的值都不是我声明的值吗?
我该如何解决呢?

这是Form1.h
#pragma once
#include "PokerTable.h"
#include "Card.h"
#include <time.h>
#include "PokerPlayer.h"
#include <fstream>
#include <string>
#include <sstream>

//global variables
//TODO make players start from 0
int firstPlayer;
int deck[52];
int nextCard=0;
PokerTable aPokerTable(9,9,1,20,30,false,false,false);
PokerPlayer players[9]; //however many players
ofstream gameLog;
/*
void setTable()
{
    aPokerTable.setNumPlayers(9);
    aPokerTable.setNumPlaying(9);
    aPokerTable.setDealerPos(1);
    aPokerTable.setBigBlind(20);
    aPokerTable.setPotSize(30);
    aPokerTable.setBetSize(20);
    aPokerTable.setFlop(false);
    aPokerTable.setTurn(false);
    aPokerTable.setRiver(false);

}
*/
string convertInt(int number) //convert to string
{
   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}

void createPlayers()
{
//  aPokerTable.setNumPlayers(9);
    for(int x=0;x<=(aPokerTable.getNumPlayers()-1);x++)
    {
        players[x] = *(new PokerPlayer(1000,(aPokerTable.getDealerPos())+1,false,0,1));//1000 chips, position i+1, not folded
    }
}



void playRound()
{
    int action;
    for(int playerTurn = firstPlayer; playerTurn <= aPokerTable.getNumPlayers()+firstPlayer;  playerTurn++)
    {
        if(players[playerTurn].getFold() == false)
        {
            if(aPokerTable.getNumPlaying() == 1)
            {
                players[playerTurn].setChipStack(players[playerTurn].getChipStack() + aPokerTable.getPotSize()); //player wins pot
            }
            else //there is more than one person playing
                {
                action = players[playerTurn].action(); //0 is check/fold, value is call/bet/raise,
                if(action > aPokerTable.getBetSize())
                {
                    aPokerTable.setBetSize(action);
                    aPokerTable.setPotSize(aPokerTable.getPotSize() + action);
                    playerTurn = playerTurn - aPokerTable.getNumPlayers();
                }
                else if (action == aPokerTable.getBetSize()) //call
                {
                    aPokerTable.setPotSize(aPokerTable.getPotSize() + action);
                }
                else //action < aPokerTable.betSize
                {
                    players[playerTurn].setFold(true);
                    aPokerTable.setNumPlaying(aPokerTable.getNumPlaying()-1); //removes player from playing tally
                }
            }
        }
    }
}


void randomDeck()
{
    int random_integer;
    int tempCard;
    //srand((unsigned)time(0));
    for(int j=0;j<=51;j++)
    {
        deck[j] = j;
    }

    for(int i=51; i>=1; i--)
    {
        random_integer = rand()%(i); //a random number between 0 and i
        tempCard = deck[i];
        deck[i] = deck[random_integer]; //put the random card from unshuffled deck into slot i of the deck
        deck[random_integer] = tempCard; //put whatever was at slot i into the random slot
    }

}

void dealCards()
{
    for(int j=1;j<=aPokerTable.getNumPlayers();j++)
    {
        players[j].setCard1(deck[nextCard]);
        nextCard++;
        players[j].setCard2(deck[nextCard]);
        nextCard++;
    }
}

void playPreFlop()
{
    aPokerTable.setBetSize(aPokerTable.getBigBlind());
    aPokerTable.setFlop(false); //it is before the flop
    aPokerTable.setTurn(false);
    aPokerTable.setRiver(false);
    randomDeck(); //shuffle cards
    dealCards();
    firstPlayer = (aPokerTable.getDealerPos() + 3)%(aPokerTable.getNumPlayers()); // first player is left of blinds between 0 and numplayers
    playRound();
}


void playFlop()
{
    aPokerTable.setFlop(true);
    firstPlayer = (aPokerTable.getDealerPos())%aPokerTable.getNumPlayers(); // first player is left of dealer between 0 and numplayers
    aPokerTable.setBetSize(0);
    playRound();
}
void playTurn()
{
    aPokerTable.setTurn(true);
    firstPlayer = (aPokerTable.getDealerPos())%aPokerTable.getNumPlayers(); // first player is left of dealer between 0 and numplayers
    aPokerTable.setBetSize(0);
    playRound();
}
void playRiver()
{
    aPokerTable.setRiver(true);
    firstPlayer = (aPokerTable.getDealerPos())%(aPokerTable.getNumPlayers()); // first player is left of dealer between 0 and numplayers
    aPokerTable.setBetSize(0);
    playRound();
    if(aPokerTable.getNumPlaying() >=2)
        {
        //showDown();
        }
}
/*
void showDown()
{

}
*/

这是pokerPlayer.h
using namespace std;
    class PokerPlayer
    {
    private:
        int chipStack,position;
        bool fold;
        int card1,card2;
    public:
        //constructors
        PokerPlayer();
        PokerPlayer(int,int,bool,int,int);

        //getters
        int getChipStack() {return chipStack;}
        int getPosition() {return position;}
        int getCard1(){return card1;}
        int getCard2(){return card2;}
        bool getFold(){return fold;}

        //setters
        void setChipStack(int inChips){chipStack = inChips;}
        void setPosition(int inPos){position = inPos;}
        void setCard1(int inCard1){card1 = inCard1;}
        void setCard2(int inCard2){card2 = inCard2;}
        void setFold(bool inFold){fold = inFold;}

        int action();
    };

    PokerPlayer::PokerPlayer()
    {
        chipStack = 1000;
        position = 0;
        fold=false;
        card1 = 0;
        card2 = 1;
    }
    PokerPlayer::PokerPlayer(int inChipStack,int inPos, bool inFold, int inCard1, int inCard2)
    {
        chipStack = inChipStack;
        position = inPos;
        fold = inFold;
        card1 = inCard1;
        card2 = inCard2;
    }
    int PokerPlayer::action()
    {
        return 0;
    }

最佳答案



请注意,为DealerPos分配了值9,这也是错误的。如果仔细观察,您会发现所有内容都移位了4个字节。

两个可能的原因。调试器可能为aPokerTable选择了错误的地址,即实际地址减去4。这不太可能。或者,如pokertable.cpp所示,PokerTable类的定义与#include the pokertable.h include文件的其他.cpp文件之间不匹配。 pokertable.cpp在numPlayers成员之前看到一个额外的成员。也许您编辑了 header 并删除了该成员,但由于某种神秘的原因而最终没有重新编译pokertable.cpp。构建+重建以修复。如果这确实起作用,请稍稍慌张。

09-11 19:19