我是C ++的新手。我写了一个给出一组状态的程序,问题是当我尝试在主程序中打印状态时,因为我的Nodo指针为[5]。我在同一函数中打印状态,并生成这些状态,问题是当我在主程序中调用该函数时,当我尝试打印某些状态时,会打印出类似912222558的数字。请帮帮我。
#include <iostream>
using namespace std;
class State {
public:
int dsc[3];
State()
{
dsc[0] = 3;
dsc[1] = 3;
dsc[2] = 1;
}
void printstate()
{
for (int i = 0; i < 3; i++) {
cout << " " << dsc[i] << " ";
}
cout << endl;
}
bool checkObjetivo()
{
if (dsc[0] == 0 && dsc[1] == 0 && dsc[2] == 0) {
return true;
}
else {
return false;
}
}
bool validState()
{
if ((dsc[0] >= 0 && dsc[0] <= 3) && (dsc[1] >= 0 && dsc[1] <= 3) && (dsc[2] >= 0 && dsc[2] <= 1) && (dsc[1] <= dsc[0])) {
return true;
}
else {
return false;
}
}
};
class Nodo {
public:
State state;
Nodo* father;
Nodo* child[5];
int level;
bool solution;
Nodo()
{
father = NULL;
for (int i = 0; i < 5; i++) {
child[i] = NULL;
}
level = 0;
solution = false;
}
Nodo childGeneration()
{
Nodo nuevoNodo;
if (state.validState()) {
if (state.checkObjetivo()) {
for (int i = 0; i < 3; i++) {
nuevoNodo.state.dsc[i] = state.dsc[i];
}
nuevoNodo.solution = true;
return nuevoNodo;
}
else {
int h0[3], h1[3], h2[3], h3[3], h4[3];
Nodo nH0, nH1, nH2, nH3, nH4;
for (int i = 0; i < 3; i++) {
h0[i] = state.dsc[i];
h1[i] = state.dsc[i];
h2[i] = state.dsc[i];
h3[i] = state.dsc[i];
h4[i] = state.dsc[i];
}
if (state.dsc[2] == 1) {
cout << "Paso dsc es 1 " << endl;
//MC
h0[0] = h0[0] - 1;
h0[1] = h0[1] - 1;
h0[2] = h0[2] - 1;
//CC
h1[1] = h1[1] - 2;
h1[2] = h1[2] - 1;
//MM
h2[0] = h2[0] - 2;
h2[2] = h2[2] - 1;
//C_
h3[1] = h3[1] - 1;
h3[2] = h3[2] - 1;
//M_
h4[0] = h4[0] - 1;
h4[2] = h4[2] - 1;
}
if (state.dsc[2] == 0) {
//MC
h0[0] = h0[0] + 1;
h0[1] = h0[1] + 1;
h0[2] = h0[2] + 1;
//CC
h1[1] = h1[1] + 2;
h1[2] = h1[2] + 1;
//MM
h2[0] = h2[0] + 2;
h2[2] = h2[2] + 1;
//C_
h3[1] = h3[1] + 1;
h3[2] = h3[2] + 1;
//M_
h4[0] = h4[0] + 1;
h4[2] = h4[2] + 1;
}
for (int i = 0; i < 3; i++) {
nH0.state.dsc[i] = h0[i];
nH1.state.dsc[i] = h1[i];
nH2.state.dsc[i] = h2[i];
nH3.state.dsc[i] = h3[i];
nH4.state.dsc[i] = h4[i];
}
cout << "nH0 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH0.state.dsc[j];
}
cout << endl;
cout << "nH1 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH1.state.dsc[j];
}
cout << endl;
cout << "nH2 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH2.state.dsc[j];
}
cout << endl;
cout << "nH3 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH3.state.dsc[j];
}
cout << endl;
cout << "nH4 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH4.state.dsc[j];
}
cout << endl;
nuevoNodo.child[0] = &nH0;
nuevoNodo.child[1] = &nH1;
nuevoNodo.child[2] = &nH2;
nuevoNodo.child[3] = &nH3;
nuevoNodo.child[4] = &nH4;
cout << "New nodo child[0] " << endl;
for (int j = 0; j < 3; j++) {
cout << nuevoNodo.child[0]->state.dsc[j];
}
cout << endl;
cout << "New nodo child[1] " << endl;
for (int j = 0; j < 3; j++) {
cout << nuevoNodo.child[1]->state.dsc[j];
}
cout << endl;
return nuevoNodo;
}
}
}
};
int main()
{
State est;
est.printstate();
Nodo nNodo;
nNodo.state = est;
Nodo nRes;
Nodo verN;
verN = nRes.childGeneration();
cout << "child[0] " << endl;
verN.child[0]->state.printstate();
return 0;
}
答案 0 :(得分:0)
您的行为不确定。
在childGeneration
函数中,您正在存储指向堆栈变量的指针。这些指针仅在该函数中有效。
您应该考虑改用动态内存分配,或者以其他方式重新考虑您的程序。