使用继承 C++ 时的错误

Posted

技术标签:

【中文标题】使用继承 C++ 时的错误【英文标题】:Errors when using inheritance C++ 【发布时间】:2013-12-06 02:50:20 【问题描述】:

所以我有一个 3D tic tac toe 游戏,它运行良好的运行一个类。当我尝试创建另一个类并将我的 cpu() 函数放在那里时,我遇到了一些问题。用户可以输入他们的选择,它可以正常工作,但是一旦 cpu() 运行,屏幕上就会打印垃圾代码而不是 x、0 或数字,但网格显示正常。

我的一堂课

class TTT 
      public: //defines functions in class
            void setup();
            void display();
            //void cpu();
            void player();
            void check(int); 

      protected: //sets variables only acessable in this class
                 int cp; //counts cpu points
            int pp; //counts player points
              char board[9]; //spots on board
              char board2[9];
              char board3[9];
              char xo[2]; //xo[0] = comp ||| xo[1] = human
              int rn; //picks who goes first
              int won;//if game has been won or tied
              int turn;//keeps track of whos turn
              int rc;//random placement by cpu
              int full;//tracks board to see if full
              int b1f; //board is full?
              int b2f;
              int b3f;


;

我试着取出 cpu();函数并将其放置在继承第一个的新类中

class CPUClass : public TTT

      public:
      void cpu();

      ;

我的新 cpu();下面的函数

void CPUClass::cpu()
//stuff happens in here

当我调用 cpu();我使用的另一个函数中的函数

CPUClass cc;
cc.cpu();

当我需要在 cpu() 函数中调用 cpu() 函数时,我只使用 cpu();

用户离开后,我的板数组的值似乎为 null 或其他东西。

如果需要,我可以发布完整的源代码,但我尝试过总结

我是不是继承错了,是不是需要加个虚函数,有什么帮助就好了。

谢谢。

根据要求提供完整代码

#include <iostream> 
#include <time.h> //seeding random
#include <windows.h> //allows for Sleep()

using namespace std;


class TTT 
      public: //defines functions in class
            void setup();
            void display();
            void cpu();
            void player();
            void check(int); 

      protected: //sets variables only acessable in this class
                 int cp; //counts cpu points
            int pp; //counts player points
              char board[9]; //spots on board
              char board2[9];
              char board3[9];
              char xo[2]; //xo[0] = comp ||| xo[1] = human
              int rn; //picks who goes first
              int won;//if game has been won or tied
              int turn;//keeps track of whos turn
              int rc;//random placement by cpu
              int full;//tracks board to see if full
              int b1f; //board is full?
              int b2f;
              int b3f;


;

class CPUClass : public TTT


      void cpu();

      ;

class CPUClass2 : public TTT

      //void cpu2();

      ;      

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


    TTT ttt; //allows class to be acessable

    cout << "Welcome to Tic-Tac-Toe!" << endl; 
    cout << "Three in a row wins [vertical, horizontal, diagonal]" << endl;

    ttt.setup();//calls setup function in class TTT




void TTT::setup() //sets up board and all variables

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

     board[0] = '1'; //sets up the board
     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'; //sets up the board
     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'; //sets up the board
     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)); //seeds with clock
     rn = (rand() %2); 

     if (rn == 0) //rnadom x and o and who goes first
            turn = 0; // cpu goes first
            xo[0] = 'x'; //comp = x
            xo[1] = 'o'; //human = o
             cout << "\nYou are 'O'" << endl; 
          else
              turn = 1; //play goes first
              xo[0] = 'o'; //comp = o
              xo[1] = 'x'; // human = x
              cout << "\nYou are 'X'" << endl; 
              

     display(); //sends to display function
     

void TTT::display()

     cout << "" << endl; 
     for (int i = 0; i < 9; i++) //for all spots on board
    


        if ( (i+1) % 3 == 0 ) // ends dont get |
        
            cout << board[i] << endl; //print number
        
        else 
        

            cout << board[i] << " | "; //print number then border
        
   

     cout << "" << endl; 
     cout << "\t";
     for (int i = 0; i < 9; i++) //for all spots on board
    


        if ( (i+1) % 3 == 0 ) // ends dont get |
        

            cout << board2[i] << endl; //print number
            cout << "\t";
        
        else 
        

            cout << board2[i] <<    " | "; //print number then border
        


   

   cout << "" << endl; 
     cout << "\t\t";
     for (int i = 0; i < 9; i++) //for all spots on board
    


        if ( (i+1) % 3 == 0 ) // ends dont get |
        

            cout << board3[i] << endl; //print number
            cout << "\t\t";
        
        else 
        

            cout << board3[i] <<    " | "; //print number then border
        


   
   cout << "" << endl;

    CPUClass cc;

    if (full == 27) //if board is full
    check(won); //check if game has been won/tied
        
    else

         if (turn%2 == 0) //decides whos turn it is
             player();
             else
                 //Comp cpu;
                 cout << "Computer is moving" ;
                 cc.cpu();//cput starts turn
          

         

     

void TTT::player()

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

     cout << "Which grid would you like to place your tile? [top=1 middle=2 bottom=3]" << endl;
     cin>> p; //which layer user wants to place

     if (p == 1)

           cout << "Enter the tile number you wish to place your marker" << endl;
           cin>>w; //in the layer of tile where to place

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

            cout << "\nSomeone has already placed there! Please select a different tile\n";
            player();//loops back

        else
              board[w-1] = xo[1];//draws what player is (x or o) on tile picked
              turn ++; //turn over
              full ++; //add one to the board
              b1f++;
              display(); //go to display
              

             

        else if (p == 2)

           cout << "Enter the tile number you wish to place your marker" << endl;
           cin>>w;

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

            cout << "\nSomeone has already placed there! Please select a different tile\n";
            player();//loops back

        else
              board2[w-1] = xo[1];//draws what player is (x or o) on tile picked
              turn ++; //turn over
              full ++; //add one to the board
              b2f++;
              display(); //go to display
              

             

     else if (p == 3)

           cout << "Enter the tile number you wish to place your marker" << endl;
           cin>>w;

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

            cout << "\nSomeone has already placed there! Please select a different tile\n";
            player();//loops back

        else
              board3[w-1] = xo[1];//draws what player is (x or o) on tile picked
              turn ++; //turn over
              full ++; //add one to the board
              b3f;
              display(); //go to display
              

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

     

  
void TTT::cpu()


     srand(time(NULL)); //seeds with clock
     rc = (rand() %9); //8 spots on board 0-8
     int bc;

     if (b1f == 9) //if a board is filled pick another one
          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); //else just randomly pick one
           

     if (bc == 0)

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

         Sleep(100); //seeded by time. So to prevent memory overflow pause before getting a new random number
         cpu(); //loops and finds a new spot

         else
           turn++; //ends turn
           full ++; //add one to board
           b1f++;
           board[rc] = xo[0]; // draw what cpu is on tile selected
           Sleep(800); //sleeps for 800ms, gives more realistic playing
           display(); //displays board
         

     

     else if (bc == 1)

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

         Sleep(100); //seeded by time. So to prevent memory overflow pause before getting a new random number
         cpu(); //loops and finds a new spot

         else
           turn++; //ends turn
           full ++; //add one to board
           b2f++;
           board2[rc] = xo[0]; // draw what cpu is on tile selected
           Sleep(800); //sleeps for 800ms, gives more realistic playing
           display(); //displays board
         

     

          else if (bc == 2)

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

         Sleep(100); //seeded by time. So to prevent memory overflow pause before getting a new random number
         cpu(); //loops and finds a new spot

         else
           turn++; //ends turn
           full ++; //add one to board
           b3f++;
           board3[rc] = xo[0]; // draw what cpu is on tile selected
           Sleep(800); //sleeps for 800ms, gives more realistic playing
           display(); //displays board
         

     

               



void TTT::check(int wo)

     //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++;
                                
          

////////////////more checking goes here, deleted because it's too long

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

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

              if (pp == cp) //if points are =
                     cout << "You tied!\n" ;
                     
              else if (pp > cp) //if player points are greater, player wins
                   cout << "You won!\n" ;
                   
              else if (pp < cp)
                   cout << "You lose!\n" ;
                   

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

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

              
     

【问题讨论】:

好吧,你不告诉我们错误是什么,那我们怎么知道?是编译时错误还是运行时错误? 我做到了,我说它玩得很好,但是一旦轮到 cpu(),电路板就会显示垃圾代码。没有其他错误。 你没有向我们展示你用来打印任何东西的代码,“垃圾代码”不一定有用。您似乎认为您的问题出在一件事上,并且给了我们很多代码,但还没有真正向任何人展示足够的东西来做出自己的诊断。当您无法自己解决问题时,您需要分享所有让您认为存在问题的内容,并让其他人根据事实做出自己的诊断,而不是基于您对可能错误的假设。 对不起,我在问我继承是否正确。添加完整代码 CPUClassCPUClass2 应该为您做什么?你没有正确地进行继承。 【参考方案1】:

继承是一种在你的主类之间建立“Is-a”关系的方法你创建一个TTT你的意思是实例化派生类CPUClass

TTT ttt; // Creates a tic tac toe board
CPUClass cc; // Creates a derived tic tac toe board 

ttt 和 cc 互不相识,他们甚至不在同一个棋盘上玩。

也许您的意思是创建一个player 类并派生一个人机/计算机变体?

我认为继承在这里不会对您有所帮助。?我认为您需要的是 PvP PvC 和 CvC 的选项?

display 循环中去掉新的板CPUClass 并使用我们所在的那个。

if (full == 27) //if board is full
   check(won); //check if game has been won/tied  

else
   if (turn%2 == 0) //decides whos turn it is
      player();  
   else
      cpu(); //cput starts turn
   

   // Should be this. 
   int currentPlayer= turn % 2; 
   if( playerIsCpu(currentPlayer) ) // create a function to return if this player is a cpu 
      cpu(currentPlayer); // cpu makes move for player 1..2
    else
      player( currentPlayer); // player 1..2 is presented with a move.
    

【讨论】:

以上是关于使用继承 C++ 时的错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 C++ 函数模板时的转换错误

使用动态二维数组时的 C++ 分段错误(核心转储)

函数调用时的 C 到 C++ 错误

继承 PortableServer::RefCountServantBase 时的编译错误

在 C++ 中使用继承的编译器错误

C++写入INI时的一个错误