-1
friend myList<T> operator +( myList<T> &c1,myList<T> &c2);

myList<T> operator +(myList<T> &c1, myList<T> &c2)
{
    int len1 = c1.getLength();
    int len2 = c2.getLength();
    int newLen = len1+len2;
    int newSize = newLen;
    T * newList = new T[newSize];
    for(int i = 0;i<len1;i++){
        newList[i] = c1.data[i];
    }
    for(int j=len1;j<newLen;j++){
        newList[j] = c2.data[j-len1];
    }
    delete c1.data;
    c1.data = newList;
    c1.size = newSize;
    c1.length = newLen;
    return *c1;
}
void main(){
    myList<int> *a = new myList<int>(5);
    myList<int> *b = new myList<int>(5);
    a+b;
}

errormessage:invalid operands of types 'myList < int >*' and 'myList< int > *' to binary 'operator+' when I call 'a+b', so how should I do to make it right ?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
joe
  • 1
  • 1
  • 1
    You have two pointers, and the operator expects references to two *non-pointers*... Consider removing the pointers and `new`... C++ classes are **not** Java classes... Try `(*a)+(*b);` – Ruks Jan 05 '19 at 04:28
  • Why are you using `new` in your `main()` function? `myList a(5); myList b(5);` You created an issue when none needed to exist by using pointers instead of plain objects in your `main` function. BTW, your program leaks memory. – PaulMcKenzie Jan 05 '19 at 04:29

1 Answers1

1

There are many wholesome ways to solve your problem, still I will name two:-

  • Dereference your two pointers...

    (*a)+(*b);
    
  • Just use normal non-pointer classes...

    myList<int> a = myList<int>(5);
    myList<int> b = myList<int>(5);
    // ...
    

Also, if you are using pointers, don't forget to free the pointer from memory using delete and assigning to nullptr... See here for more information...

Ruks
  • 3,886
  • 1
  • 10
  • 22