[英]Boolean Function Giving a Seg Fault
我正在嘗試為 Tictactoe 游戲中的所有可能動作制作 DFA。 當我第二次調用我的 firstMove() 函數時,我遇到了段錯誤,我不確定為什么。
這是我的 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;
}
這就是打印 Independents State Parent Position 0: 2 Position 1: 2 Position 2: 2 Position 3:2 Position 4:2 Position 5:2 Position 6:2 Position 7:2 Position 8:2 Yes 在迭代子項中狀態子位置 0: 1 位置 1: 2 位置 2: 2 位置 3: 2 位置 4: 2 位置 5: 2 位置 6: 2 位置 7: 2 位置 8: 2 否 Independents Segmentation fault (core dumped)
這是我調試時的錯誤
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
_children
從vector<state>*
更改為vector<state>
應該可以解決您的問題。
當您使用它時,通過將其更改為State::State(const State & s)
使其成為適當的復制構造State::State(const State & s)
。 另外,使position
成為一個靜態數組,以及: int position[9]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.