I am trying to test/debug a simple c++ code with template class and copy constructor. I define a vector class (user defined not STL) with 2 constructors one for initialization of vector elements and another for assigning values from an array. First I call the initialization constructor ,the code runs fine ,but when I call the constructor with a float array elements , the very first assignment statement hits SIGSEGV error , I have checked all values and addresses in debugger , no hints found. I give below the code ,I give below the sequence of calls and line details of error , from main vector() is called ,no issues , then v1=inputf1; is invoked ,then the copy constructor vector(T1* t2) is invoked , there code hits SIGSEGV error at very first assignment stmt in the loop that is t[i]=t2[i]; ,please help understand, since the code is small you can as well try executing and let me know -Thanks.
#include <stdio.h>
#include <iostream>
using namespace std;
const int size = 4;
template <class T1>
class vector {
T1* t;
public:
vector() {
t = new T1[size];
for(int i=0;i<size;i++)
t[i] =0;
}
vector(T1* t2) {
for(int i=0;i<size;i++)
t[i]=t2[i];//SIGSEGV error at this stmt//
}
T1 operator * (vector &va) {
T1 sum=0;
for(int i=0;i<size;i++)
sum += this->t[i]*va.t[i];
return sum;
}
};
int main(int argc, char **argv)
{
int ip;
float ipf;
float inputf1[4] = {1.2,2.3,3.4,4.5};
vector<float> v1;
v1=inputf1;
float inputf2[4] = {5.6,6.7,7.8,8.9};
vector<float> v3;
v3=inputf2;
int inputi1[4] = {1,2,3,4};
vector<int> v2;
v2=inputi1;
int inputi2[4] = {5,6,7,8};
vector<int> v4;
v4=inputi2;
ip = v2*v4;
ipf = v1*v3;
cout<<"inner product of int vectors = "<< ip <<endl;
cout<<"inner product of float vectors = "<< ipf <<endl;
return 0;
}