2015-08-24 34 views
-1

當我將8個計算機類指針傳遞給Tournament類並試圖操作這些指針時,我遇到了分段錯誤。錦標賽等級需要8名電腦派生玩家,並將調用Refree Function來決定每位玩家的勝利數量。 例如:輸入:比賽聯賽(電腦*播放器1,*播放器2 ...等) 輸出:分段故障或不打印任何東西。爲什麼我使用指針獲取分段錯誤?

class Computer{ 
     public: 
      Computer(); 
      Computer(int number); 
      int get_c_number(); 
      friend class Refree; 
      friend class Tournament; 
     public: 
      string c_game; 
      int c_number; 
      static int no_of_win; 
      string c_name; 
    }; 
    #include "computer.h" 
    Computer::Computer(){ 

    } 
    //====computer.cpp 
    int Computer::no_of_win=0; 
    Computer::Computer(int number){ 
     c_number = number; 

     for(int i=0; i<c_number; i++) 
     { 
      c_game.push_back('R'); 
     } 

    } 
    int Computer::get_c_number() 
    { 
     return c_number; 
    } 
    //=====example computer child class== 
    #ifndef _TOOLBOX_H 
    #define _TOOLBOX_H 
    #include "computer.h" 
    #include <string> 
    #include <iostream> 

    class Toolbox:public Computer{ 
     public: 
      Toolbox(); 
      Toolbox(int number); 
     private: 


    }; 
    #endif // 
    #include "toolbox.h" 
    Toolbox::Toolbox(){}; 
    Toolbox::Toolbox(int number){ 
     c_number = number; 

     for(int i=0; i<c_number; i++) 
     { 
      c_game.push_back('S'); 
     } 


    }; 
    //=========tournament class== 
    #define _TOURNAMENT_H 
    #include "computer.h" 
    #include "refree.h" 
    #include <string> 
    #include <iostream> 
    #include <time.h> 
    #include <ctime> 
    #include <stdlib.h> 
    class Tournament:public Computer{ 
     public: 
      Tournament(); 
      Tournament(Computer *A, Computer *B, Computer *C, Computer *D, Computer *E, Computer *F, Computer *G, Computer *H); 
      void decision(); 

     private: 
      static int number_of_games; 
      Computer *tournament[8]; 
      Computer *tournament2[4]; 
      Computer *tournament3[2]; 


    }; 
    #endif //_TOURNAMENT_H 
    //======tournament.cpp=== 
    #include "tournament.h" 
    int Tournament::number_of_games = 5; 
    Tournament::Tournament(Computer *A, Computer *B, Computer *C, Computer *D, Computer *E, Computer *F, Computer *G, Computer *H){ 
     tournament[0] = A; 
     tournament[1] = B; 
     tournament[2] = C; 
     tournament[3] = D; 
     tournament[4] = E; 
     tournament[5] = F; 
     tournament[6] = G; 
     tournament[7] = H; 
    }; 

    void Tournament::decision(){ 
     Refree number1(tournament[0], tournament[1]), number2(tournament[2], tournament[3]), number3(tournament[4], tournament[5]), number4(tournament[6], tournament[7]); 
     number1.decision2(); 
     number2.decision2(); 
     number3.decision2(); 
     number4.decision2(); 
     for(int i=0; i<4; i++) 
     { 
      if(tournament[(2*i)]->no_of_win > tournament[(2*i)+1]->no_of_win) 
      { 
       tournament[(2*i)]->no_of_win = 0; 
       tournament[(2*i)+1]->no_of_win = 0; 
       tournament2[i] = tournament[(2*i)]; 
      } 
      else if(tournament[(2*i)]->no_of_win < tournament[(2*i)+1]->no_of_win) 
      { 
       tournament[(2*i)]->no_of_win = 0; 
       tournament[(2*i)+1]->no_of_win = 0; 
       tournament2[i] = tournament[(2*i)+1]; 
      } 
     } 


      Refree number5(tournament2[0], tournament2[1]), number6(tournament2[2], tournament2[3]); 
      number5.decision2(); 
      number6.decision2(); 

     for(int i=0; i<2; i++) 
     { 
      if(tournament2[(2*i)]->no_of_win > tournament2[(2*i)+1]->no_of_win) 
      { 
       tournament2[(2*i)]->no_of_win = 0; 
       tournament2[(2*i)+1]->no_of_win = 0; 
       tournament3[i] = tournament2[(2*i)]; 
      } 
      else if(tournament2[(2*i)]->no_of_win < tournament2[(2*i)+1]->no_of_win) 
      { 
       tournament2[(2*i)]->no_of_win = 0; 
       tournament2[(2*i)+1]->no_of_win = 0; 
       tournament3[i] = tournament2[(2*i)+1]; 
      } 
     } 
     Refree number7(tournament3[0], tournament3[1]); 
     number7.decision2(); 
     if(tournament3[0]->no_of_win > tournament3[1]->no_of_win) 
     { 
      cout<<tournament3[0]->c_name<<endl; 
     } 
     else if(tournament3[0]->no_of_win < tournament3[1]->no_of_win) 
     { 
      cout<<tournament3[1]->c_name<<endl; 
     } 

    }; 
    //============main.cpp=========== 
    string user_input; 
     getline(cin, user_input); 

     std::vector<std::string> vec; 

     istringstream iss(user_input); 
     copy(istream_iterator<string>(iss), 
     istream_iterator<string>(), 
     back_inserter(vec)); 
     Computer *players[8]; 
     for(int i=0; i<8; i++) 
     { 
      if(vec.at(i)=="Avalanche") 
      { 
       players[i] = new Avalanche(5); 
       players[i]->c_name = "Avalanche"; 
       //cout<<"Avalanche: "<<players[i]->c_game<<endl; 
      } 
      else if(vec.at(i)=="Bureaucrat") 
      { 
       players[i] = new Bureaucrat(5); 
       players[i]->c_name = "Bureaucrat"; 
       // cout<<"Bureaucrat: "<<players[i]->c_game<<endl; 
      } 
      else if(vec.at(i)=="Toolbox") 
      { 
       players[i] = new Toolbox(5); 
       players[i]->c_name = "Toolbox"; 
       // cout<<"Toolbox: "<<players[i]->c_game<<endl; 
      } 
      else if(vec.at(i)=="Crescendo") 
      { 
       players[i] = new Crescendo(5); 
       players[i]->c_name = "Crescendo"; 
       // cout<<"Crescendo: "<<players[i]->c_game<<endl; 
      } 
      else if(vec.at(i)=="Paperdoll") 
      { 
       players[i] = new Paperdoll(5); 
       players[i]->c_name = "Paperdoll"; 
       //cout<<"Paperdoll: "<<players[i]->c_game<<endl; 
      } 
      else if(vec.at(i)=="FistfullODollars") 
      { 
       players[i] = new Fistfullodollars(5); 
       players[i]->c_name = "FistfulloDollars"; 
       //cout<<"FistfullOdollars: "<<players[i]->c_game<<endl; 
      } 
     } 
     Tournament *thebigdayin = new Tournament(players[0], players[1], players[2], players[3], players[4], players[5], players[6], players[7]); 
     thebigdayin->decision(); 
+4

sscce會很好。見www.sscce.org – Bathsheba

+0

對不起,我的骯髒的粘貼:(我是新的堆棧溢出 –

+0

什麼將所有的指針?你不能使用矢量? – NathanOliver

回答

0

我懷疑所發生的事情(這是很難確認您還沒有提供SSCCE)在第一輪過後,你的一些球員有相同數量的勝利,並沒有輸入兩個if或低於else if

if(tournament[(2*i)]->no_of_win > tournament[(2*i)+1]->no_of_win) 
{ 
    tournament[(2*i)]->no_of_win = 0; 
    tournament[(2*i)+1]->no_of_win = 0; 
    tournament2[i] = tournament[(2*i)]; 
} 
else if(tournament[(2*i)]->no_of_win < tournament[(2*i)+1]->no_of_win) 
{ 
    tournament[(2*i)]->no_of_win = 0; 
    tournament[(2*i)+1]->no_of_win = 0; 
    tournament2[i] = tournament[(2*i)+1]; 
} 

而一些第二輪的球員都離開了初始化。你需要決定什麼是決勝局。