给出分段故障的布尔函数



我正试图为Tictactoe游戏中所有可能的动作制作DFA。当我第二次调用firstMove()函数时,我得到了一个seg错误,我不知道为什么。

这是我的States.hpp文件。

#ifndef STATE_HPP
#define STATE_HPP
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 class State{
 public:
  State();
  State(State *s);
  //~State;                                                                                                                                              
  State getState();
  void setStateChildren(State *s);
  vector<State*> *getChildren();
  void setFirstMove(bool b);
  void setPosition(int *p[]);
  void setfirst();
  void setFirstMove();
  bool firstMove();
  string getFirstPlayer();
  int *position();
  bool isFinal();
  bool isReject();
  void setFinal(bool f);
  void setReject(bool r);
  void print();
 private:
  bool final;
  bool reject;
  bool firstPlayerMove;
 // bool tie;                                                                                                                                           
 //  vector<int> * _position;                                                                                                                           
  int *_position = new int[9];// = {2, 2, 2, 2, 2, 2, 2, 2, 2};                                                                                          
  vector<State *> *_children;
};

#endif

这是我的States.cpp文件。

State::State(){
  final = false;
  reject = false;
  for (int i = 0; i < 9; i++)
    _position[i] = 2;
  firstPlayerMove = true;
  _children = new vector<State *> ();
};
State::State(State *s){
  this->final = s->final;
  for( int i = 0; i < 9; i++)
  this->_position[i] = s->_position[i];
  firstPlayerMove = false;
  // this->firstPlayerMove = s->firstPlayerMove;                                                                                                         
  this->_children = s->_children;
  final = false;
  reject = false;
}
void State::setStateChildren(State *s){
  _children->push_back(s);
}
vector<State*> *State::getChildren(){
  return _children;
}
void State::setFirstMove(bool b){
  firstPlayerMove = b;
}
bool State::firstMove(){
  return firstPlayerMove;
}
void State::setPosition(int *p[]){
  //  cout << *_position[0] << endl;                                                                                                                     
  for (int i = 0; i < 9; i++){
    cout << *p[i] << endl;
    _position[i] = *p[i];
  }
  //_position = p;                                                                                                                                       
}
int *State::position(){
  return _position;
}
bool State::isFinal(){
  return final;
}
bool State::isReject(){
  return reject;
}
void State::setFinal(bool f){
  final = f;
}
void State::setReject(bool r){
  reject = r;
}
void State::print(){
  for(int i = 0; i < 9; i++)
    cout << "Position " << i << ": " << _position[i] << endl;
  if (firstPlayerMove)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

}

这是我的Main.cpp

void CreateDFA(State *state, int n){
  bool first;
  dependents(state, state->firstMove());
  if (n == 0)
    return;
  if (state->isFinal())
    return;
  if (state->isReject())
    return;
  cout << "State Parent " << endl;
  state->print();
  for (vector<State*>::iterator iter = state->getChildren()->begin(); iter !=    state->getChildren()->end(); iter++){
     cout << " In iteration of children" << endl;
     cout << "State Child" << endl;
     (*iter)->print();
     first =  (*iter)->firstMove();
     dependents(*iter, first);
     CreateDFA(*iter, n - 1);
       }
 }
void dependents(State *state, bool first){
  cout << "In dependents" << endl;
  int symbol;
  if (first == true)
    symbol = 1;
  else
    symbol = 0;
  int count = 0;
  while (count < 3){
     if (state->position()[count] == 2){
  // If move is blank, it creates a new State called child and changes that position to the symbol                                                   
  // then adds that child to state's children                                                                                                        
     State *child = new State(state);
     child->setFirstMove(!(first));
     child->position()[count] = symbol;
     state->setStateChildren(child);
   }
  count++;
  }
}
int main(){
  State * s = new State();
  CreateDFA(s, 3);
  return 0;
}

这就是正在打印的内容在从属关系中State Parent位置0:2位置1:2位置2:2位置3:2位置4:2位置5:2位置6:2位置7:2位置8:2对在孩子的迭代中State Child位置0:1位置1:2位置2:2位置3:2位置4:2位置5:2位置6:2位置7:2位置8:2不在从属关系中分段故障(堆芯转储)

这是我调试时的错误

0x0000000000400e14 in State::firstMove (this=0x0) at States.cpp:36
36    return firstPlayerMove;
#0  0x0000000000400e14 in State::firstMove (this=0x0) at States.cpp:36
#1  0x0000000000401a20 in CreateDFA (state=0x0, n=2) at Main.cpp:16
#2  0x0000000000401b77 in CreateDFA (state=0x615c20, n=3) at Main.cpp:30
#3  0x0000000000401ce2 in main () at Main.cpp:67

在您的"复制构造函数"中,您说:

this->_children = s->_children;

但是因为_children是指向向量(而不是实际向量)的指针,所以它只是让两个State对象指向同一向量。当你改变其中一个时,它也会改变另一个。

State中将_childrenvector<state>*更改为vector<state>应该可以解决您的问题。

当您使用它时,将其更改为State::State(const State & s),使其成为一个合适的复制构造函数。另外,将position设为静态数组:int position[9]

相关内容

  • 没有找到相关文章

最新更新