Jump to content

getting a 'vector subscript out of range' error

- - - - -

  • Please log in to reply
3 replies to this topic

#1
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
The program compiles but when it reaches transport () function I get the 'vector subscript out of range'.
It also mentions line 932 which does not exist nor has it existed previously. I have tried to find out the reason for this error but cannot work it out.
Any help would be great thanks.




#include <iostream>

#include "cstdio"

#include <cctype>

#include <ctime>

#include <string>

#include <vector>

using namespace std;


class Allveh {

  public:

    Allveh() : 

      dg(0),

	  id(),

	  carry()


{}


	 int dg;

	 char id;

	 char carry;

};


class Struck : public Allveh {

  public:

    Struck()

    {

	 dg = 3;

     id = 's';

	 carry = 'n';

	}

};


class Svan76 : public Allveh {

  public:

    Svan76()

    {

	 dg = 3;

     id = 'h';

	 carry = 'n';

	}

};


class Xveh {

  public:

    Xveh() : 

      dg(0),

	   id(),

	  carry()


{}

	 int dg;

	 char id;

	 char carry;

};


class P3van : public Xveh {

  public:

    P3van()

    {

	 dg = 4;

     id = 'p';

	 carry = 'n';

	}

};


vector<Allveh> allvector;

vector<Allveh>::iterator it;


vector<Xveh> xvector;

vector<Xveh>::iterator iter;



int allCreator(Allveh& unit)

{

		int num;

		 cout << "Enter 1-3 vehicle objects to put into vector.\n";

		

		 cin>> num;

		 

		  for (int i = 0; i<num; i++)

			 {

			allvector.push_back(unit);

			cout <<"Issue Idtag = "<< allvector.size()-1<<" to those objects."<<endl;

			 }				 				

 return 0 ;

 }


int xCreator(Xveh& unit)

{

		int num;

		 cout << "Enter 1-3 vehicle objects to put into vector.\n";

		

		 cin>> num;

		 

		  for (int i = 0; i<num; i++)

			 {

			xvector.push_back(unit);

			cout <<"Issue Idtag = "<< xvector.size()-1<<" to those objects."<<endl;

			 }

 return 0 ;

 }



int factory()

{

	

	cout<<"Create 'p3-van' objects. \n";

	P3van p3vanobj; xCreator (p3vanobj);


	cout<<"Create 'S-struck' objects. \n";

	Struck struckobj; allCreator (struckobj);


	cout<<"Create '76 s-truck' objects. \n";

	Svan76 svan76obj; allCreator(svan76obj);


return 0;

}


template < class T2 >

int transport (vector<T2> &gen2vector ) 

{

	int yesno = 'y'||'n';

	cout<<"Do you wish to transport?\n";

	cin>> yesno;

	if (yesno=='y') 

		{

			int entry;

			cout <<"Enter ID tag.\n";

			cin>>entry;

			cout<<"Transporter type is "<< gen2vector[entry].id <<endl;

			gen2vector[entry].carry = 'y';

			cout<<"Is it transporting? "<<gen2vector[entry].carry<<endl;

			cout<<"now return to movement function.\n";

		}

	

	if (yesno=='n') 

		{

			cout<<"returning to movements function.\n";

		}

	

return 0;

}



template < class T >

void movements (vector<T> &genvector) 

{

    int entry;

	cout <<"Select ID tag.\n";

	cin>>entry;

	cout<<"Vehicle type in movements is "<< genvector[entry].id<<endl;

	transport(genvector);

	cout<<"finished with movements function.\n";

}


int main ()

{

	factory();

	

	char anykey;

	cout <<"Send S-trucks to movements. Press any key.\n";

	cin>> anykey;

	movements(allvector);


	char any;

	cout <<"Send P3 x-van to movements. Press any key.\n";

	cin>> any;

	movements (xvector);

	system ("pause");

return 0;

}





#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
It compiles for me without any errors or crashes, and I think transport function isn't working properly, or I just don't understand it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Thats right. It compiles fine but transport function doesnt work when I run it and comes up with that error. I dont see why it wouldnt work.?????

#4
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Solved it. Perhaps int yesno='y'||'n'; should read char yesno = 'y'||'n';
Stupid mistake...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users