I have this section of code that keeps giving a vector subscript out of range error, but I cant figure out why.

the vector is defined as

vector< vector<double> > distance;

for the test I am doing, point->size() = 3, and through the debugger I have verified that the matrix does get resized to 3 by 3, however when it gets to [2][2] it throws the vector subscript out of range error. How do I fix this?

//resize matrix
distance.resize(point->size());
		for (int i = 0; i < point->size(); i++)
			distance[i].resize(point->size());

		for (int i = 0; i < point->size(); i++)
			{
			for (int j = 0; j	< point->size(); j++)		
				{	
				cout << i << ", " << j << endl;
				distance[i][j] = *removed*;
				}
			}

Can you post a compilable example of your problem?

How big is the vector inside the vector? The main vector is of size 3 so the subscript [2] should be fine but I don't see anything for the size of each vector in distance. what would happen if you accesses distance[1][2]. Does that throw an exception?

No, [1][2] does not throw an exception, [2][2] is the only one, and Ive also tried making the vectors bigger than they need to be as well as statically making it 10 by 10, with the same error at [2][2]

I'm kinda thinking this is a bug in with microsofts vector.h in visual studio 2010

could you please post your whole code where you set up your matrix?

#include <iostream>
#include <math.h>
#include <vector>
#include <utility>

using namespace std;

class tsp1
	{
	private:
	double totalDistance;
	vector<pair<double,double>> *point; //points = x,y respectivly
	vector< vector<double> > distance;
void distantiate()
		{
		//resize the matrix
		distance.resize(point->size());
		for (int i = 0; i < point->size(); i++)
			distance[i].resize(point->size());

		for (int i = 0; i < point->size(); i++)
			{
			for (int j = 0; j	< point->size(); j++)		//make a matrix of all the distances from point to point
				{	// sqrt |Pythagorean theorm|
				cout << i << ", " << j << endl;
				distance[i][j] = sqrt( 
					fabs( 
					pow( ((point->at(j).first) - (point->at(i).first)),2) + 
					pow( ((point->at(j).second) - (point->at(i).second)),2)));
				}
			}
		}

void add(long x, long y)
		{
		point->push_back(pair<double,double>(x,y));
		}

};

That is all the code that deals with the matrix right now

Can you post a compilable example of your problem?

that code above is compilable

First, you need to put a space between the two angle brackets:

vector<pair<double,double> > *point; //points = x,y respectivly

instead of

vector<pair<double,double>> *point; //points = x,y respectivly

It should not only compile (which it doesn't):

(.text+0x18): undefined reference to `main'

but also have a simple use case - an example where we can compile, run, and see the error.

commented: Formating of a program is the programmers choice and when is related to a single space should not be remarked on +0

1. compilers ignore white space
2. Writing a simple main that implements that takes less than a minute so I figured thats not a big deal

#include <iostream>
#include <math.h>
#include <vector>
#include <utility>

using namespace std;

class tsp1
	{
	public:
	double totalDistance;
	vector<pair<double,double>> *point; //points = x,y respectivly
	vector<<vector<double>> distance;
	vector<int> order;
	//long TWO;

	void distantiate()
		{
		//resize the matrix
		distance.resize(point->size());
		for (int i = 0; i < point->size(); i++)
			distance[i].resize(point->size());

		for (int i = 0; i < point->size(); i++)
			{
			for (int j = 0; j	< point->size(); j++)		//make a matrix of all the distances from point to point
				{	// sqrt |Pythagorean theorm|
				cout << i << ", " << j << endl;
				distance[i][j] = sqrt( 
					fabs( 
					pow( ((point->at(j).first) - (point->at(i).first)),2) + 
					pow( ((point->at(j).second) - (point->at(i).second)),2)));
				}
			}
		}

tsp1() : totalDistance(0)
		{
		point = new vector<pair<double,double>>;
		}

	void add(long x, long y)
		{
		point->push_back(pair<double,double>(x,y));
		}
};

int main()
	{
	tsp1 tsp;

	tsp.add(1,1);
	tsp.add(2,2);
	tsp.add(3,1);
	tsp.distantiate();

	return 0;
	}

I solved my issue by not using vectors and instead defining distance as

double **distance;

You're right, compilers do ignore white space, but they do not ignore symbols - I think it interprets >> as the instream operator.

vector<pair<double,double>> *point; //points = x,y respectivly

produces

error: '>>' should be '> >' within a nested template argument list

on g++4.4.1.

Just because it works on your compiler doesn't mean it is right.

Dave

commented: Some rep back http://www.comeaucomputing.com/techtalk/templates/#shiftshift +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.