Bus error

I have this class matrix that consist of a vector of vector of int:
 
vector<vector<int> > rows;

anyway I overloaded the increment operator on it
1
2
3
4
5
6
7
8
9
10
11
12
matrix operator++(matrix &one, int)
{
	matrix answer (one);
	for (int i = 0; i < one.length; i++)
	{
		for (int j = 0; j < one.length; j++)
		{
			one.rows[i][j] = one.rows[i][j] + 1;
		}
	}
	return answer;
}


and tried to run my main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 *  main.cpp
 *  
 *
 *  Created by Matthew Dunson on 12/7/09.
 *  Copyright 2009 The Ohio State University. All rights reserved.
 *
 */

#include "matrix.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main (int argc, char *argv[])
{
	string line;
	ifstream file;
	file.open (argv[1]);
	int matrix_length;
	getline (file, line);
	matrix_length = atoi(line.c_str());
	matrix matrixOne (matrix_length);
	cout << matrixOne++ << endl;
	cout << matrixOne << endl;
	file.close ();
}


However, I get a bus error at run time. The error occurs when main executes line 27:


111
111
111
Bus error


I think it has something to do with how I'm implementing my increment operator, but I don't know what it is. Can you please help me (tell me what is wrong)?
Last edited on
Look at what you are modifying and what you are returning:

1
2
3
4
			one.rows[i][j] = one.rows[i][j] + 1;
		}
	}
	return answer;


Second, consider using iterators when looping over vectors, not indexes.
Yeah, I did that on purpose (I don't know if it is right). Take the piece of code:
 
	cout << matrixOne++ << endl;


According to the increment operation this would output the original value of matrixOne and then increment matrixOne. That's
why I am modifying matrixOne and returning a copy of it's original.
Topic archived. No new replies allowed.