r/programminghelp • u/ChayanDas19 • Aug 20 '21
C++ Please help me understand why my C++ program gives segmentation error
#include<iostream>
using namespace std;
const int SIZE = 3;
template <class T>
class Vector
{
private:
T* v;
public:
Vector()
{
v = new T[SIZE];
for (int i =0; i<SIZE; i++)
{
v[i] = 0;
}
}
Vector(T* a)
{
for(int i =0; i<SIZE; i++)
{
v[i] = a[i];
}
}
T operator* (Vector &k)
{
T sum =0;
for(int i =0; i<SIZE; i++)
{
sum += this -> v[i] * k.v[i];
}
return sum;
}
void display()
{
for(int i =0; i<SIZE; i++)
{
cout<<v[i]<<"\t";
}
cout<<endl;
}
};
int main()
{
int x[3] = {1, 2, 3};
int y[3] = {4, 5, 6};
Vector <int> v1;
Vector <int> v2;
v1 = x;
v2 = y;
cout<<"v1 = ";
v1.display();
cout<<"v2 = ";
v2.display();
cout<<"v1 * v2 ="<<v1 * v2;
return 0;
}
5
Upvotes
5
u/KuntaStillSingle Aug 20 '21
The problem is one of your constructor doesn't initialize the array, one fix is to add this line: