In the code below I am trying to convert int * type to vector class. But upon running the program, program terminates after a halt in the conversion constructor to copy values from int[] into vector::int *v.
Also, this is an example from one of the books. I have been trying to figure out what is happening here. Any leads would be helpful.
#include<iostream>
using namespace std;
const int size=3;
template <class T>
class vector{
T*v;
public:
vector()
{
v=new T[size];
for(int i=0; i<size; i++){
cout << "A" << endl;
v[i]=0;
}
cout << "vector 0-arg ctor" << endl;
}
vector(T *a) ///////////////////// conversion ctor for type conversion
{
for(int i=0; i<size; i++){
cout << "B" << endl;
v[i]=a[i];////////////////// Program exits here
}
cout << "vector 1-arg ctor" << endl;
}
T operator*(vector &y)
{
T sum=0;
for(int i=0; i<size; i++){
cout << "C" << endl;
sum += this->v[i] * y.v[i];
}
return sum;
}
};
int main(){
int x[3]={1,2,3};
int y[3]={4,5,6};
vector <int> v1;
vector <int> v2;
v1=x; ////////////////////// Type conversion from int * -> vector
v2=y; ////////////////////// int * -> vector
int R=v1*v2;
cout << "R : " << R << endl;
return 0;
}
The output is below ->
A
A
A
vector 0-arg ctor
A
A
A
vector 0-arg ctor
B