Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c++ - error in type T contructor | do I need address constructor? no match for ‘operator=’ (operand types are ‘abc<int>’ and ‘abc<int>*’)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I fixed it. now my vectorClass constructor is like this

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 not any more

               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

           }

and I had to include construtor this takes address of this

  abc(T& obj)
    {
      this=obj;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...