Hi,

I'm having a problem with vectors, its the first time i'm using this structure so i'm probably missing something obvious.

What i'm trying to do is, create a class with a vector as a member, my code compiles with no errors but 4 warnings ... also to do with another vector.

the code compiles, but when debugging there is an error: vector<T> too long... also it says std::length_error at memory location 0x0030f1d8..

Just to explain my code, i'm trying to implement a bfs search for the 8 puzzle problem. I feel logically my code is correct and so i have not posted everything. Also it is a lot to read, but i have posted all the bits concerning the vectors.

I would appreciate any help!! thanks in advance!

here is my code:

my header file:
Code:
#ifndef HEADER_H
#define HEADER_H

#include <vector>
using std::vector;
class node{

public:
		int state[3][3];
		int id;
		char action;
		vector<int> list;
		node* parent;
		node();
		~node();
};


#endif
file with definitions:
Code:
#include "header.h"
#include <iostream>

node::node() { list.reserve(10); }
node::~node() { }
and the main file
Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include "header.h"

int choice;
int i,j,x,y,a,b,c,d,aa, children, start, stop, child;
int InState [3][3];
int GoalState [3][3];
bool leave = false;
char moves[4];
std::vector<int> final;

using namespace std;
int main(){


	GoalState[0][0] = 1;
	GoalState[0][1] = 2;
	GoalState[0][2] = 3;
	GoalState[1][0] = 8;
	GoalState[1][1] = 0;
	GoalState[1][2] = 4;
	GoalState[2][0] = 7;
	GoalState[2][1] = 6;
	GoalState[2][2] = 5;

	cout<<"Welcome to the 8-puzzle"<<endl;
		
	cout<<"Our goal state is :"<<endl;
	cout<<endl;
	for (i=0; i<3; i++){
		cout<<GoalState[i][0]<<" "<<GoalState[i][1]<<" "<<GoalState[i][2]<<endl;
	}


	cout<<"\nEnter Initial State one integer at a time:"<<endl;
	cout<<"Please use integers 0 to 8 only where 0 represents the blank tile"<<endl;
	
	for (i = 0; i<3; i++) { 
		for (j=0; j<3; j++){
			cin>> InState[i][j];
			if (InState[i][j] > 8) {
				cout<<"number should be from 0 to 8. Re-enter"<<endl;
				cin>> InState[i][j];}

		}
	}

	//at this point we find the position of zero or the blank
	for (i = 0; i<3; i++) { 
		for (j=0; j<3; j++){
			if (InState[i][j] == 0){
				x = i;
				y = j;
			}
		}
	}

	cout<<"x "<< x <<"y "<< y <<endl;

	cout<<"The initial state selected is"<<endl;
	for (i=0; i<3; i++){
		cout<<InState[i][0]<<" "<<InState[i][1]<<" "<<InState[i][2]<<endl;
	}

	//reserve for final path
	final.reserve(10);
	
	//define the root node
	node root;
	for (int i = 0; i<3; i++) { 
		for (int j=0; j<3; j++){
			root.state[i][j] = InState[i][j];
		}
	}
	root.id=0;
	//start vector list to find path to goal
	root.list.push_back(root.id);
	//find available moves for root
	zero_state(x,y,moves);

	/*cout<<"Available moves:"<<endl;
	for (i=0; i<sizeof(moves); i++){
		cout<<moves[i]<<endl;
	}*/
	aa=sizeof(moves);
	a = aa;
	//generate number of children from root, using the number of available moves
	node *levela;
	try {
		levela = new node[aa];
	} catch (bad_alloc xa) {
		cout<<"Allocation Failure\n";
		return 1;
	}

	if (leave!=true){

	//********************put this in a do while loop.... must continue until goal is found*******

	//max depth?????

	//for each child of root generate their children the same way
	for (int i=0; i<aa; i++){
		//fill each node with required information 
		levela[i].id = i+1;
		//find available moves for each child
		zero_position(levela[i].state,x,y);
		zero_state(x,y,moves);
		//define their parent
		levela[i].parent=&root;
		//define their states
		copy(levela[i].state,root.state);
		swap_tiles(levela[i].state,moves[i],x,y);
		//define path to state
		levela[i].list=root.list;
		levela[i].list.push_back(levela[i].id);
		// total number of children for all nodes at this level
		children+=sizeof(moves);
		//determine if goal state reached?
		if (repeat_goal(levela[i].state,GoalState)) {
					final=levela[i].list;
					leave=true;
					break;
				}
	}
	}
		
		//define children for each node 
		start=aa;
		int bb=children;
		node *levelb;
		try {
			levelb = new node[bb];
		} catch (bad_alloc xa) {
			cout<<"Allocation Failure\n";
			return 1;
		}

		if (!leave){
		
		children=0;
		for (int j=0; j<start; j++){
			//determine parent node moves
			zero_position(levela[j].state,x,y);
			zero_state(x,y,moves);
			child=sizeof(moves);
			aa+=child;
			// total number of children for all nodes at this level
			children+=sizeof(moves);
			for (int jj=0; jj<child;jj++){
				//fill each node with required information
				levelb[jj].id = start+jj;
				//define their parent
				levelb[jj].parent=&levela[j];
				//define their states
				copy(levelb[jj].state,levela[j].state);
				swap_tiles(levelb[jj].state,moves[jj],x,y);
				//define path to state
				levelb[jj].list=levela[j].list;
				levelb[jj].list.push_back(levelb[jj].id);
				
				//determine if goal state reached?
				if (repeat_goal(levelb[jj].state,GoalState)) {
					final=levelb[jj].list;
					leave=true;
					break;
				}
			}
		}
		b=aa-start;
		}
		
		//define children for each node 
		int cc=children;
		node *levelc;
		//levelc = new node[cc];
		try {
			levelc = new node[cc];
		} catch (bad_alloc xa) {
			cout<<"Allocation Failure\n";
			return 1;
		}
		if (!leave){
		stop=aa;
		
		children=0;
		for (int k=start; k<stop; k++){
			//determine parent node moves
			zero_position(levelb[k].state,x,y);
			zero_state(x,y,moves);
			child=sizeof(moves);
			aa+=child;
			// total number of children for all nodes at this level
			children+=sizeof(moves);
			for (int kk=0; kk<child;kk++){
				//fill each node with required information
				levelc[kk].id = stop+kk;
				//define their parent
				levelc[kk].parent=&levelb[k];
				//define their states
				copy(levelc[kk].state,levelb[k].state);
				swap_tiles(levelc[kk].state,moves[kk],x,y);
				//define path to state
				levelc[kk].list=levelb[k].list;
				levelc[kk].list.push_back(levelc[kk].id);
				
				//determine if goal state reached?
				if (repeat_goal(levelc[kk].state,GoalState)) {
					final=levelc[kk].list;
					leave=true;
					break;
				}
			}
		}
		c = aa-stop;
		}

		//define children for each node 
		int dd=children;
		node *leveld;
		//leveld = new node[dd];
		try {
			leveld = new node[dd];
		} catch (bad_alloc xa) {
			cout<<"Allocation Failure\n";
			return 1;
		}
		if (!leave){		
		start=stop;
		stop=aa;
		children=0;
		for (int k=start; k<stop; k++){
			//determine parent node moves
			zero_position(levelc[k].state,x,y);
			zero_state(x,y,moves);
			child=sizeof(moves);
			aa+=child;
			// total number of children for all nodes at this level
			children+=sizeof(moves);
			for (int kk=0; kk<child;kk++){
				//fill each node with required information
				leveld[kk].id = stop+kk;
				//define their parent
				leveld[kk].parent=&levelc[k];
				//define their states
				copy(leveld[kk].state,levelc[k].state);
				swap_tiles(leveld[kk].state,moves[kk],x,y);
				//define path to state
				leveld[kk].list=levelc[k].list;
				leveld[kk].list.push_back(leveld[kk].id);
				
				//determine if goal state reached?
				if (repeat_goal(leveld[kk].state,GoalState)) {
					final=leveld[kk].list;
					leave=true;
					break;
				}
			}
		}
		d = aa-stop;
		}
		
		if (leave){
			cout<<"The solution is "<<endl;
			display(root.state);
			for (int i=0; i<a; i++){
				for (int j=0; j<final.size();j++){
					if (final[j] == levela[i].id){
						display(levela[i].state);
				}
			}
			}
			for (int i=0; i<b; i++){
				for (int j=0; j<final.size();j++){
					if (final[j] == levelb[i].id){
						display(levelb[i].state);
				}
			}
			}
			for (int i=0; i<c; i++){
				for (int j=0; j<final.size();j++){
					if (final[j] == levelc[i].id){
						display(levelc[i].state);
				}
			}
			}
			
			for (int i=0; i<d; i++){
				for (int j=0; j<final.size();j++){
					if (final[j] == leveld[i].id){
						display(leveld[i].state);
				}
			}
			}
		}
			
	delete levela;
	delete levelb;
	delete levelc;
	delete leveld;

	system ("pause");
	
	return 0;
}