C++ 给出Seg故障的布尔函数

C++ 给出Seg故障的布尔函数,c++,class,pointers,segmentation-fault,C++,Class,Pointers,Segmentation Fault,我正在尝试为Tictatcoe游戏中所有可能的动作制作DFA。当我第二次调用firstMove()函数时,我得到一个seg错误,我不知道为什么 这是我的States.hpp文件 #ifndef STATE_HPP #define STATE_HPP #include <string> #include <vector> #include <iostream> using namespace std; class State{ public: Sta

我正在尝试为Tictatcoe游戏中所有可能的动作制作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
在“复制构造函数”中,您说:

this->_children = s->_children;
但是因为
\u children
是指向向量(而不是实际向量)的指针,所以它只是让两个状态对象指向同一个向量。当你改变一个,它也改变了另一个

状态下将
子项
向量*
更改为
向量
,应该可以解决您的问题


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

调试器告诉您什么?没有什么突然出现在我身上--发布完整的代码,可以重现问题和调试器崩溃时的堆栈跟踪,请不要访问地址0x2处的内存您的复制构造函数不正确,但这并没有导致崩溃:State::State(State*s)应该是State::State(const State&s)堆栈跟踪的其余部分是什么?那是什么函数?什么叫那个函数,等等,保留向量可以吗?是的,这就是你想要的。但是您真的想在这一行复制父级:
State*child=newstate(State)或您想要一个新的状态对象吗?如果复制_children向量,则在复制时,您的子对象的子对象将与父对象的子对象相同(尽管后续更改不会共享)。我确实希望创建新的状态对象,但我希望将父对象的值设置为新的状态对象。不过,我不需要来自父对象的_子对象位于新对象中。那还会是一个复制构造函数吗?不,你不会想要的。复制构造函数应该创建一个副本,而不是一个“有点类似”的对象。所以在这种情况下,我不会将它从state*改为const state&,但是你不想复制向量,所以(假设你把它变成了一个向量而不是向量*),你只需要对_个孩子做任何事情,它就会是空的
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;
}
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;