我正在尝试为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;
}
这就是印刷 在家属 州长 位置0:2 位置1:2 位置2:2 位置3:2 位置4:2 位置5:2 位置6:2 位置7:2 位置8:2 是 在迭代的孩子 国家儿童 位置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
答案 0 :(得分:1)
在“复制构造函数”中,您说:
this->_children = s->_children;
但是因为_children
是一个指向向量(不是实际向量)的指针,它只是让两个State对象都指向同一个向量。当你改变一个时,它会改变另一个。
在_children
中将vector<state>*
从vector<state>
更改为State
可以解决您的问题。
当你在它时,通过将其更改为State::State(const State & s)
使其成为正确的复制构造函数。另外,将position
设为静态数组:int position[9]