当我将8个计算机类指针传递给Tournament类并试图操纵这些指针时,我遇到了分段错误。锦标赛级别需要8个计算机派生的玩家,并将调用Refree功能来决定每个玩家的胜利数量。 例如:输入:锦标赛联赛(电脑*播放器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();
答案 0 :(得分: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];
}
一些第二轮选手未被初始化。你需要决定决胜局应该是什么。