I am creating template class for vector from standard library in C++. And I am getting error at this line
arr[i]=new T(_px,_py,_pz);
above line is inside constructor of vectorClass
.
this is the code of vectorClass
vectorClass(J _px[],J _py[],J _pz[],int cap)
{
arr= (T *)malloc(cap*sizeof (T));
for(int i=0;i<cap;i++)
arr[i]=new T(_px,_py,_pz);//error line
std::cout<<"vectorClass(J _px,J _py,_J _px,cap) called"<<std::endl;
T(_px,_py,_pz);
//how to allocated cap sized T arr[cap] and assign it to arr
}
arr
is declared as T* arr;
in the note of conpiler is says
note: no known conversion for argument 1 from ‘abc<int>*’ to ‘const abc<int>&’
Does the above error tells about that I need constructor in template abc
that accepts address of this
like
abc(this& ?) //?what should be the name? its just (this)
this is my full code
#include <iterator>
#include <iostream>
#include <cstring>
template <typename T> class abc{
private:
abc *p;
T *px=x;
T *py=y;
T *pz=z;
int capacity=10;
int current1=0;
public:
//these members must be included
T x[10]={};
T y[10]={};
T z[10]={};
abc(const T _px[],const T _py[],const T _pz[]):x{{10}},y{{10}},z{{10}}
{
std::cout<<"---> abc(_px,_py,_pz) called"<<std::endl;
memcpy(x,_px,sizeof (T));
memcpy(y,_py,sizeof (T));
memcpy(z,_pz,sizeof (T));
}
int writetoHW()
{
//member write to hardware...x,y,z... linux socket, mmap and other work
return 0;
}
};
template <class T>
class vectorClass
{
T* arr;
int capacity=10;
int current=0;
public:
//in main user will pass three arrays
template<class J>
vectorClass(J _px[],J _py[],J _pz[],int cap)
{
arr= (T *)malloc(cap*sizeof (T));
for(int i=0;i<cap;i++)
arr[i]=new T(_px,_py,_pz);//error line
// test.cpp:57:26: error: no match for ‘operator=’ (operand types are ‘abc<int>’ and ‘abc<int>*’)
//test.cpp:7:29: note: candidate: ‘constexpr abc<int>& abc<int>::operator=(const abc<int>&)’
std::cout<<"vectorClass(J _px,J _py,_J _px,cap) called"<<std::endl;
T(_px,_py,_pz);
//how to allocated cap sized T arr[cap] and assign it to arr
}
vectorClass(int x){}
void push_back(T t)
{
T *temp;
//arr=new T[++capacity];//how to increase the size of arr by one with ...(T t)
//temp[capacity] = t;
//for (int i = 0; i < capacity + 1; ++i){
// arr[i] = temp[i];
//}
delete [] temp;
}
/*
REST of the class remains the same
*/
};
int main()
{
int a1[3]={1,1,1};
//const abc<int> a2(a1,a1,a1);
vectorClass<abc<int>> o(a1,a1,a1,3);
//a.push_back(a2);
return 0;
}
See Question&Answers more detail:
os