Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在类之间访问数据成员时出现问题_C++_Class_Organization - Fatal编程技术网

C++ 在类之间访问数据成员时出现问题

C++ 在类之间访问数据成员时出现问题,c++,class,organization,C++,Class,Organization,我有两个班;一个球员班和一个比赛班。我希望玩家能够使用其所有成员函数;携带、捕获等,但其成员函数需要了解匹配类数据成员;董事会、对手、挑战者,但不公开这些数据 如果你找不到一个好的解决方案或者对我的问题感到困惑,我仍然会对班级组织的建议感兴趣 mancala.h #ifndef MANCALA_H #define MANCALA_H #include <string> #include <random> namespace mancala{ class

我有两个班;一个球员班和一个比赛班。我希望玩家能够使用其所有成员函数;携带、捕获等,但其成员函数需要了解匹配类数据成员;董事会、对手、挑战者,但不公开这些数据

如果你找不到一个好的解决方案或者对我的问题感到困惑,我仍然会对班级组织的建议感兴趣

mancala.h

#ifndef MANCALA_H
#define MANCALA_H

#include <string>
#include <random>

namespace mancala{  

    class player{
        public:
            void carry(int pit);
            void capture(const int& pit);
            bool madeStore(const int& pit)const;
            bool madeCapture(const int& pit)const; 
            bool madeValidMove(const int& pit)const;
            bool madeSideClear()const;
            bool operator==(const player& p)const;
        private:
            std::string tag;
    };

    class match{
        public:
            match(const int& match_ID = 0) : board{4,4,4,4,4,4,0,4,4,4,4,4,4,0}{
                if(match_ID){
                    ;
                }
                else{
                    this->turn = randPlayer();
                    this->status = "make move";
                }
            };
            void begin();
            void end();
            void save();
            bool update();
        private:
            player challenger, opponent, turn;
            static const int seeds = 48;
            static const int pits  = 14;
            std::string status;
            int board[pits];
            void nextTurn();
            player randPlayer();
    };
}

#endif//MANCALA_H

让match类成为玩家类的朋友

C++中的一个朋友类可以访问它被声明为朋友的类的“私有”和“保护”成员。

void player::carry(int pit)
{
    if(this->madeValidMove(pit) == false){
        status = "invalid move";
    }
    else{

        int hand = board[pit];

        board[pit] = 0;

        for(hand; hand != 0; hand--){  

            if(pit == 13){
                pit = 0;
            }
            else{
                pit++;  
            }
             //Skips opponent store
            if(pit == 13 && turn == challenger){
                pit = 0;
            }//Skips challenger store
            else if(pit == 6 && turn == opponent){
                pit++;
            }
             //If placing last seed check where it is being placed
            if(hand == 1){
                 //Store
                if(this->madeStore(pit)){
                    board[pit]++;
                    status = "free move";
                }//Capture
                else if(this->madeCapture(pit)){
                    this->capture(pit);
                  nextTurn();
                }//Normal
                else{
                    board[pit]++;
                    nextTurn();
                }
            }//Placing seed
            else{
                board[pit]++;
            }
        }

        if(this->madeSideClear()){
            end();
        }
    }   

    save();
}