错误说类定义为public是私有的 [英] error saying class defined as public is private

查看:178
本文介绍了错误说类定义为public是私有的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用类继承制作3D Tic Tac Toe游戏。编译器错误如下。



行:



43 [Error]'void CPUClass :: cpu是私人的



190


$ b 查看我的代码,我已经清楚地定义了cpu ()作为public。任何想法我可以忽略什么?

  #include< iostream> 
#include< time.h> // seeding random
#include< windows.h>

using namespace std;


class TTT {
public:
void setup();
void display();
void cpu();
void player();
void check(int);

protected:
int cp; // comp points
int pp; // player points
char board [9]; // board
char board2 [9];
char board3 [9];
char xo [2];
int rn; //随机的第一个
int won; //检查win或stalemate
int turn; //跟踪当前玩家
int rc; //随机comp移动
int full; //看板是否已满
int b1f;
int b2f;
int b3f;


};

class CPUClass:public TTT {


void cpu();

};

int main(int argc,char * argv [])
{

TTT ttt;

cout<< 新奇的3D TicTacToe! << endl;
cout<< ~~玩起来好像所有三个板被堆叠成一个立方体,同样的规则适用〜<< endl;

ttt.setup();


}

void TTT :: setup(){//设置游戏空间

won = 0; // game not won
full = 0; // board is full at 9
cp = 0;
pp = 0;
b1f = 0;
b2f = 0;
b3f = 0;

//三个板:
board [0] ='1';
board [1] ='2';
board [2] ='3';
board [3] ='4';
board [4] ='5';
board [5] ='6';
board [6] ='7';
board [7] ='8';
board [8] ='9';

board2 [0] ='1';
board2 [1] ='2';
board2 [2] ='3';
board2 [3] ='4';
board2 [4] ='5';
board2 [5] ='6';
board2 [6] ='7';
board2 [7] ='8';
board2 [8] ='9';

board3 [0] ='1';
board3 [1] ='2';
board3 [2] ='3'
board3 [3] ='4';
board3 [4] ='5';
board3 [5] ='6';
board3 [6] ='7';
board3 [7] ='8';
board3 [8] ='9';

srand(time(NULL));
rn =(rand()%2);

if(rn == 0){
turn = 0; // comp go first
xo [0] ='x'; // comp = x
xo [1] ='o'; // human = o
cout<< \\\
You are'O'<< endl;
} else {
turn = 1; // player goes first
xo [0] ='o'; // comp = o
xo [1] ='x'; // human = x
cout<< \\\
You are'X'<< endl;
}

display(); //发送这个信息到显示函数
}

void TTT :: display(){/ ******你在2:10离开了这里修改**** ** /

cout<< < endl;
for(int i = 0; i <9; i ++)
{


if((i + 1)%3 == 0)
{
cout<<板[i] endl;
}
else
{

cout<板[i] |;
}
}

cout<< < endl;
cout<< \t;
for(int i = 0; i <9; i ++)
{


if((i + 1)%3 == 0)
{

cout<< board2 [i]<< endl;
cout<< \t;
}
else
{

cout< board2 [i]<< |;
}


}

cout<< < endl;
cout<< \t\t;
for(int i = 0; i <9; i ++)
{


if((i + 1)%3 == 0)
{

cout<< board3 [i]<< endl;
cout<< \t\t;
}
else
{

cout< board3 [i]<< |;
}


}
cout< < endl;

CPUClass cc;

if(full == 27){//如果板子满了
check(won); //检查游戏是否赢得或绑定
}
else {

if(turn%2 == 0){
player();
} else {
cc.cpu();
}

}

}

void TTT :: player(){

int w;
int p;
while(won!= 1)//如果没有人赢过
{

cout< 做你的动作:选择一个层[top = 1 middle = 2 bottom = 3]< endl;
cin>> p;

if(p == 1){

cout< 做你的举动:选择一个空间<< endl;
cin>> w;

if((board [w-1] =='x')||(board [w-1] =='o')){//检查是否开放

cout<< \\\
Space occupied,Invalid move \\\
;
player();

} else {
board [w-1] = xo [1]; //绘制当前玩家移动
turn ++;
full ++;
b1f ++;
display(); // go to display
}

}

else if(p == 2){

cout< 做你的举动:选择一个空间<< endl;
cin>> w;

if((board2 [w-1] =='x')||(board2 [w-1] =='o')){//检查是否开放

cout<< \\\
Space occupied,Invalid move \\\
;
player();

} else {
board2 [w-1] = xo [1];
turn ++;
full ++;
b2f ++;
display();
}

}

else if(p == 3){

cout< 做你的举动:选择一个空间<< endl;
cin>> w;

if((board3 [w-1] =='x')||(board3 [w-1] =='o')){//检查是否开放

cout<< \\\
Space occupied,Invalid move \\\
;
player();

} else {
board3 [w-1] = xo [1];
turn ++;
full ++;
b3f;
display();
}

}
else {
cout<<请选择一个选项!\\\
\\\
;
player();
}

}

}
void TTT :: cpu(){


srand (空值)); // seed with clock
rc =(rand()%9);
int bc;

if(b1f == 9){//如果一个电路板被填满了一个opthers
bc = 1+(rand()%2);
}
else if(b3f == 9){
bc =(rand()%2);
}
else if(b2f == 9){
bc =(rand()%2);
if(bc == 1){
bc = 2;
}
}
else {
bc =(rand()%3); //如果没有满,随机选择一个
}

if(bc == 0){

if((board [rc] =='x' )||(board [rc] =='o')){

cpu(); //循环并找到一个新位置

} else {
turn ++;
full ++;
b1f ++;
board [rc] = xo [0];
display();
}

}

else if(bc == 1){

if((board2 [rc] =='x ')||(board2 [rc] =='o')){

cpu(); //循环并找到一个新位置

} else {
turn ++;
full ++;
b1f ++;
board [rc] = xo [0];
display();
}

}

else if(bc == 2){

if((board3 [rc] =='x ')||(board3 [rc] =='o')){

cpu(); //循环并找到一个新位置

} else {
turn ++;
full ++;
b1f ++;
board [rc] = xo [0]
display();
}
}
}

void TTT :: check(int wo)// check wins
{
// horizo​​ntal win
if((board [0] =='x')&&(board [1] =='x')&&
$ b if(rn == 0){
cp ++;
} else {
pp ++;
}

}


if((board [6] =='x')&& 'x')&&&(board3 [2] =='x')){


if(rn == 0){
cp ++;
} else {
pp ++;
}
}

wo = 1;
cout<< \\\
Game Over!\\\
;

cout<< \\\
Score:Player - << pp<<Computer - << cp<<\\\
;

if(pp == cp){// if points are =
cout<< STALEMATE\\\
;
}
else if(pp> cp){
cout< YOU WIN\\\
;
}
else if(pp< cp){
cout< 计算机wins\\\
;
}

int c;
cout<<\\\
Play again?[1] for yes other else to exit\\\
;
cin>> C;

if(c == 1){//如果用户进入1,重置游戏
setup();
}
else {
exit(0);

}
}


解决方案

默认情况下,类访问是私有的。如果要通过 CPUClass 对象调用 CPUClass :: cpu()函数,则需要使 cpu() public:

  class CPUClass:public TTT {
public ://<< ----- make below functions public
void cpu();
};


Making a 3D Tic Tac Toe game using class inheritance. Compiler error as follows.

line:

43 [Error] 'void CPUClass::cpu()' is private

190 [Error] within this context

Looking at my code, I've clearly defined cpu() as public. Any ideas what I'm overlooking?

#include <iostream> 
#include <time.h> //seeding random
#include <windows.h>

using namespace std;


class TTT {
      public: 
            void setup();
            void display();
            void cpu();
            void player();
            void check(int); 

      protected:
              int cp; //comp points
              int pp; //player points
              char board[9]; //board
              char board2[9];
              char board3[9];
              char xo[2]; 
              int rn; //random who goes first
              int won;//checks for win or stalemate
              int turn;//keeps track of current player
              int rc;//random comp move
              int full;//see if board is full
              int b1f; 
              int b2f;
              int b3f;


};

class CPUClass : public TTT{


      void cpu();

      };

int main(int argc, char *argv[])
{

    TTT ttt; 

    cout << "Newfangled 3D TicTacToe!" << endl; 
    cout << "~~play as though all three boards are stacked as a cube. Same rules apply.~~" << endl;

    ttt.setup();


}

void TTT::setup(){ //sets up gamespace

     won = 0;  //game not won 
     full = 0; //board is full at 9
     cp = 0;
     pp =0;
     b1f = 0;
     b2f = 0;
     b3f=0;

//the three boards:
     board[0] = '1';
     board[1] = '2';
     board[2] = '3';
     board[3] = '4';
     board[4] = '5';
     board[5] = '6';
     board[6] = '7';
     board[7] = '8';
     board[8] = '9';

     board2[0] = '1';
     board2[1] = '2';
     board2[2] = '3';  
     board2[3] = '4';
     board2[4] = '5';
     board2[5] = '6';
     board2[6] = '7';
     board2[7] = '8';
     board2[8] = '9';

     board3[0] = '1';
     board3[1] = '2';
     board3[2] = '3';
     board3[3] = '4';  
     board3[4] = '5';
     board3[5] = '6';
     board3[6] = '7';
     board3[7] = '8';
     board3[8] = '9';

     srand(time(NULL));
     rn = (rand() %2); 

     if (rn == 0){ 
            turn = 0; // comp goes first
            xo[0] = 'x'; //comp = x
            xo[1] = 'o'; //human = o
             cout << "\nYou are 'O'" << endl; 
          }else{
              turn = 1; //player goes first
              xo[0] = 'o'; //comp = o
              xo[1] = 'x'; // human = x
              cout << "\nYou are 'X'" << endl; 
              }

     display(); //send this info to display function
     }

void TTT::display(){/******you left off revising right here at 2:10******/

     cout << "" << endl; 
     for (int i = 0; i < 9; i++)
    {


        if ( (i+1) % 3 == 0 )
        {
            cout << board[i] << endl;
        }
        else 
        {

            cout << board[i] << " | ";
        }
   }

     cout << "" << endl; 
     cout << "\t";
     for (int i = 0; i < 9; i++)
         {


        if ( (i+1) % 3 == 0 )
        {

            cout << board2[i] << endl;
            cout << "\t";
        }
        else 
        {

            cout << board2[i] <<    " | "; 
        }


   }

   cout << "" << endl; 
     cout << "\t\t";
     for (int i = 0; i < 9; i++)
    {


        if ( (i+1) % 3 == 0 ) 
        {

            cout << board3[i] << endl; 
            cout << "\t\t";
        }
        else 
        {

            cout << board3[i] <<    " | ";
        }


   }
   cout << "" << endl;

    CPUClass cc;

    if (full == 27){ //if board is full
    check(won); //check if game has been won or tiedtied
        }
    else{

         if (turn%2 == 0){ 
             player();
             }else{
                 cc.cpu();
          }

         }

     }

void TTT::player(){

  int w;
  int p;
  while (won != 1) //if no one has won
  {

     cout << "Make your move: Choose a layer [top=1 middle=2 bottom=3]" << endl;
     cin>> p; 

     if (p == 1){

           cout << "Make your move: Choose a space" << endl;
           cin>>w;

           if ((board[w-1] == 'x') || (board[w-1] == 'o')){ //checks if spot is open

            cout << "\nSpace occupied, Invalid move\n";
            player();

        }else{
              board[w-1] = xo[1];//draws current players move
              turn ++;
              full ++;
              b1f++;
              display(); //go to display
              }

           }  

        else if (p == 2){

           cout << "Make your move: Choose a space" << endl;
           cin>>w;

           if ((board2[w-1] == 'x') || (board2[w-1] == 'o')){ //checks if spot is open

            cout << "\nSpace occupied, Invalid move\n";
            player();

        }else{
              board2[w-1] = xo[1];
              turn ++;
              full ++;
              b2f++;
              display(); 
              }

           }  

     else if (p == 3){

           cout << "Make your move: Choose a space" << endl;
           cin>>w;

           if ((board3[w-1] == 'x') || (board3[w-1] == 'o')){ //checks if spot is open

            cout << "\nSpace occupied, Invalid move\n";
            player();

        }else{
              board3[w-1] = xo[1];
              turn ++;
              full ++;
              b3f;
              display(); 
              }

           } 
           else{
                cout <<"Please select one of the options!\n\n" ; 
                player();
                } 

     }

  }
void TTT::cpu(){


     srand(time(NULL)); //seeds with clock
     rc = (rand() %9);
     int bc;

     if (b1f == 9){ //if a board is filled go one of the opthers
          bc = 1+(rand() %2);
      }
      else if (b3f == 9){
           bc = (rand() %2);
           }
      else if (b2f == 9){
           bc = (rand() %2);
           if (bc == 1){
                  bc = 2;
                  }
           }
      else {
           bc = (rand() %3); //randomly pick one if none are full
           }

     if (bc == 0){

     if ((board[rc] == 'x' ) || ( board[rc] == 'o')){

         cpu(); //loops and finds a new spot

         }else{
           turn++;
           full ++; 
           b1f++;
           board[rc] = xo[0]; 
           display(); 
         }

     }

     else if (bc == 1){

          if ((board2[rc] == 'x' ) || ( board2[rc] == 'o')){

         cpu(); //loops and finds a new spot

           }else{
           turn++;
           full ++; 
           b1f++;
           board[rc] = xo[0]; 
           display(); 
         }

     }

          else if (bc == 2){

               if ((board3[rc] == 'x' ) || ( board3[rc] == 'o')){

         cpu(); //loops and finds a new spot

         }else{
           turn++;
           full ++; 
           b1f++;
           board[rc] = xo[0]; 
           display(); 
         }
     }
               }

void TTT::check(int wo)// check wins
{
     //horizontal win
      if ((board[0] == 'x') && (board[1] == 'x') && (board[2] == 'x')){

                   if (rn == 0){
                          cp++;
                          }else{
                                pp++;
                                }

                   }


      if ((board[6] == 'x') && (board2[4] == 'x') && (board3[2] == 'x')){


          if (rn == 0){
                          cp++;
                          }else{
                                pp++;
                                }
          }

              wo =1;
              cout << "\nGame Over!\n" ;

              cout << "\nScore: Player - "<<pp<<" Computer - "<<cp<<"\n" ;

              if (pp == cp){ //if points are =
                     cout << "STALEMATE\n" ;
                     }
              else if (pp > cp){
                   cout << "YOU WIN\n" ;
                   }
              else if (pp < cp){
                   cout << "The computer wins\n" ;
                   }

              int c;
           cout<<"\nPlay again? [1] for yes anything else to exit\n" ;
           cin >> c;

           if (c == 1){ //if user enters 1, reset the game
                 setup();
                 }
           else{
           exit(0);

              }
     }

解决方案

By default, class access is private. If you want to call CPUClass::cpu() function through a CPUClass object, you need to make cpu() public:

class CPUClass : public TTT{
public:            //<<----- make below functions public
      void cpu();
};

这篇关于错误说类定义为public是私有的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆