Я начинаю с верхнего узла дерева глубиной 5 слоев и рекурсивно вызываю getvalue () для каждого.Каждый узел связан с двумя узлами на следующем уровне.Я уверен, что это не моя проблема, так как я дважды проверил алгоритм на бумаге.Однако, как только я доберусь до уровня 3, это вызовет ошибку сегментации.С помощью valgrind я понял, что он возник, когда я пытался напечатать переменную класса oper
.Я понятия не имею, куда идти с этим, поэтому ваша помощь очень ценится.Это код:
class Node {
public:
vector<Node> children;
long constval;
char oper;
void setconst();
Node();
void copy(const Node*);
int getvalue();
private:
int mult(int,int);
int div(int,int);
int add(int,int);
int sub(int,int);
};
Node::Node() {
bool c = false;
vector<char> operations;
operations.push_back('m');
operations.push_back('a');
operations.push_back('s');
operations.push_back('d');
operations.push_back('c');
constval = rand();
int randnum = rand() % 5;
cout << randnum << "\n";
oper = operations[randnum];
}
int Node::getvalue() {
cout << oper << '\n';
if (oper == 'm') {
return Node::mult(children[0].getvalue(), children[1].getvalue());
}
else if (oper == 'd') {
return Node::div(children[0].getvalue(), children[1].getvalue());
}
else if (oper == 'a') {
return Node::add(children[0].getvalue(), children[1].getvalue());
}
else if (oper == 's') {
return Node::sub(children[0].getvalue(), children[1].getvalue());
}
else if (oper == 'c') {
return constval;
}
}
РЕДАКТИРОВАТЬ: Вот мой инициализирующий алгоритм:
class Individual {
public:
vector< vector<Node> > nodes;
vector< vector<Node> > getrand();
void replace(vector< vector<Node> >);
void mutate(double);
double run();
Individual();
};
Individual::Individual() {
nodes.resize(5);
nodes[0].resize(1);
int size = 2;
for(int i = 1; i < 5; i++) {
nodes[i].resize(size);
size = size * 2;
}
vector<char> operations;
operations.push_back('a');
operations.push_back('s');
operations.push_back('d');
operations.push_back('m');
nodes[0][0].oper = operations[rand() % 4];
for(int x = 0; x < nodes[4].size(); x++) {
nodes[4][x].setconst();
}
for(int i = 0; i < 4; i++) {
for(int x = 0; x < nodes[i].size(); x++) {
nodes[i][x].children.push_back(nodes[i+1][x*2]);
nodes[i][x].children.push_back(nodes[i+1][x*2+1]);
}
}
}