2

I write a class template, but operators work only when two types are same. I do not know how to define to types in header file and cpp file. This is my code:

header file:

ArrayList& operator=(ArrayList const&);

cpp file:

template <class T>
ArrayList<T>& ArrayList<T>::operator=(ArrayList<T> const& other) {
    if (this != &other) {
        delete [] Array;
        Array = new T [other.lenght];
        lenght = other.lenght;
        std::copy(other.Array, other.Array+lenght, Array);
    }
    return *this;
}

if I define a and b as int, a=b works. but if I define a as a char, a=b does not work. how to solve this?

EDITED::

As Barry said, we must change the syntax. And I must change my instantiations at the end of the .cpp file (I am using separate .h and .cpp file). But the problem is from this part

 if (this != &other) {
    delete [] Array;
    Array = new T [other.lenght];
    lenght = other.lenght;
    std::copy(other.Array, other.Array+lenght, Array);
}

but I do not know where is the problem, if I comment above, everything works good, but...

I got these errors:

error C2440: '!=' : cannot convert from 'const ArrayList *' to 'ArrayList *const '

error C2248: 'ArrayList::lenght' : cannot access private member declared in class 'ArrayList' (3 times)

Barry
  • 286,269
  • 29
  • 621
  • 977
Ramyad
  • 107
  • 1
  • 8
  • Do you mean you want to assign an `ArrayList` to an `ArrayList`? – Joseph Mansfield Apr 02 '13 at 20:27
  • 4
    You might want to have a look at [this related question](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – juanchopanza Apr 02 '13 at 20:28
  • yes, I know I should define to classes but I when I do that I get erros – Ramyad Apr 02 '13 at 20:28
  • @juanchopanza this is not my question, I know this, my class works well for two ArrayList – Ramyad Apr 02 '13 at 20:29
  • 1
    I know it is not your question, I am just pointing out an obvious error in your code. – juanchopanza Apr 02 '13 at 20:30
  • @juanchopanza I add these `template class ArrayList;` ... at the end of .cpp file to avoid that ;) – Ramyad Apr 02 '13 at 20:34
  • Soon you will be asking why your code doesn't compile, so when that happens, have a look at the link in my first comment. – juanchopanza Apr 02 '13 at 20:37
  • @juanchopanza yes! I saw that, because I had the same problem. But I have solved that according to the 2nd answer in that question. My programs compiles well after adding those instantiations at the the end of the .cpp file – Ramyad Apr 02 '13 at 20:40
  • Regarding the added question (after edit): What data types are involved? (I.e., what are `T` and `U` instantiated to?) When you do `std::copy(other.Array,other.Array+lenght,Array)` copies of the individual elements will be performed, but if the data types are not compatible, that won't work. – jogojapan Apr 03 '13 at 07:20
  • @jogojapan for example int and char. other.Array is char and Array is int. If I comment std::copy the error will exist – Ramyad Apr 03 '13 at 07:46
  • @Ramyad What is the error message exactly? – jogojapan Apr 03 '13 at 07:47
  • @jogojapan I have many "don't know things"! but the one with error is this: ` 1>c:\users\ideapad\documents\visual studio 2012\projects\oop-exer-hw03-04\oop-exer-hw03-04\arraylist.h(85): error C2446: '!=' : no conversion from 'const ArrayList *' to `` – Ramyad Apr 03 '13 at 07:59
  • @jogojapan and this one 1>c:\users\ideapad\documents\visual studio 2012\projects\oop-exer-hw03-04\oop-exer-hw03-04\arraylist.h(87): error C2248: 'ArrayList::lenght' : cannot access private – Ramyad Apr 03 '13 at 08:01
  • @jogojapan and 2 other errors like above one – Ramyad Apr 03 '13 at 08:02
  • @Ramyad Please add the error messages to the question. Anyway, the fact that `lenght` is private is a problem. You either need a friend declaration, or make it public, or use an accessor function. (The reason why this is necessary is that `ArrayList` and `ArrayList` are really two different types. They cannot access each other's private members.) – jogojapan Apr 03 '13 at 09:23

1 Answers1

6

In your header, instead of:

ArrayList& operator=(ArrayList const&);

Make it

template <typename U>
ArrayList& operator=(ArrayList<U> const&);

That should accept any kind of U on the right hand side. Similarly, the cpp file should use this awesome construction:

template <typename T>
template <typename U>
ArrayList<T>& ArrayList<T>::operator=(ArrayList<U> const& other) {
    // clearly uninteresting details here
    return *this;
}
Barry
  • 286,269
  • 29
  • 621
  • 977