Overfloading operator* for matrix_vector multiplication

Hello everyone. I writed a program to multiply a matrix with a vector using operator*. When I compiled this program, it did not run completely. This warning is: " Fault: access violation at 0x4011ca: write of address 0x40b804" or contructor function " vector::vector(float *)". I don't know why. I hope anybody help me, figure out for me. I really thank for help!

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  /* vectormat.cpp */

#include <iostream.h>
#include <conio.h>

class matrix; 

class vector
{
   static int n; 
   float *v; 
public:
   vector();
   vector(float *);
   vector(vector &);
   ~vector();
   void display();
   static int & Size(){return n;};
   friend vector operator*(matrix &, vector &);
   friend class matrix;
};

int vector::n=0;


vector::vector()
{
   int i;
   v=new float[n];
   for(i=0;i<n;i++)
   {
   	cout<<"The element is "<<(i+1)<<" : ";
      cin>>v[i];
   }
}

vector::vector(float *a)
{
	for(int i=0;i<n;i++)
   	v[i]=a[i];
}

vector::vector(vector &b)
{
	int i;
   for (i=0;i<n;i++)
   	v[i]=b.v[i];
}

vector::~vector()
{
	delete v;
}

void vector::display() 
{
	for(int i=0;i<n;i++)
   	cout<<v[i]<<" ";
   cout<<"\n";
}

// lop matrix
class matrix
{
   static int n; 
   vector *m; 
public:
   matrix();
   matrix(matrix &); 
   ~matrix();
   void display();
   static int &Size() {return n;};
   friend vector operator*(matrix &, vector &);
};

int matrix::n=0;


matrix::matrix()
{
  //	int i;
   m=new vector[n];
}

matrix::matrix(matrix &b)
{
	int i,j;
   m=new vector[n];
   for(i=0;i<n;i++)
   	for(j=0;j<n;j++)
      	  m[i].v[j]=b.m[i].v[j];
}

matrix::~matrix()
{
	delete m;
}

void matrix::display()
{
	for(int i=0;i<n;i++)
   	m[i].display();
}

// operator * function matrix and vector
vector operator*(matrix &m,vector &v)
{
   float *a= new float[vector::Size()];
   int i,j;
   for(i=0;i<matrix::Size();i++)
   {
   	a[i]=0;
        for(j=0;j<vector::Size();j++)
      	  a[i]+=m.m[i].v[j]*v.v[j];
   }
  	return vector(a);
  	//return a;

}

void main()
{
   clrscr();
   int size;
   cout<<"Distance of vector "; cin>> size;
   vector::Size()=size;
   matrix::Size()=size;
   cout<<"Create a vector \n";
   vector v;
   cout<<" v= \n";
   v.display();
   cout<<"Create a matrix \n";
   matrix m;
   cout<<" m= \n";
   m.display();
   cout<<" Result of m*v is: \n";
   vector u=m*v; // operator m*v
   u.display();
   getch();
}
The segmentation fault happens on line 137, which calls the function on line 106, which (on line 116), calls the constructor on line 37 which assigns to v[i] on line 40, which HAS NOT BEEN ALLOCATED, with a 'new'.
The vector(a) will exist at that point, and have it's float *v member variable, which will point somewhere random, as it has not been initialized. When v[i] gets assigned on line 40, it will cause the segmentation fault. ie the program write somewhere it should not.
Last edited on
Thank @ShodanHo ! I understood my problem. But can you help me? You can show me how do I repair this program?
Well, I do have to ask- is this for an assignment, practice, or just the general desire to get a functional matrix class up and running?

As for the problem, just do as you did in the default constructor- make v a new float of size n, where n is the size of... well, you can figure that part out.
Other problems are:
(1) memory leak at line 108.
(2) int matrix::n=0; static int &Size() {return n;}; This is truly awful. Only one matrix size can be used in the application, and it must match the vector size.

It is probably better to throw the code away, and start again.
Thank @ShodanDo, @Ispil very much. I repaired my program and it runned completly.
Topic archived. No new replies allowed.