-1

We have just been introduced to generics and templates in my comp sci class. I have been asked to create a generic container class that supports storage and retrieval of any data type. I have all of my functions working okay, except when it comes to resize my array. I have the call for resize inside my insert function:

template< typename T >
void Container< T >::insert( T  magic)
{
    if (index == size)
    {
        resize();
    }
    containerPtr[index] = magic;
    index++;
}

The size variable is the size of the array and the index is the next insert location.

and here is my resize function:

template< typename T >
void Container< T >::resize()
{
    int doubSize = size * 2;
    Container< T > temp(doubSize);
    for (int i = 0; i < size; i++)
    {
        temp[i] = containerPtr[i];      // error 1 here
    }
    *containerPtr = temp;               // error 2 here
    size = doubSize;
}

My overloaded =

template< typename T >
const T Container< T >::operator=(const T& rhs)
{
    if(this == &rhs)
    {
        return *this;
    }
    return *rhs;
}

I receive the following errors when I try to compile:

1: error C2676: binary '[': 'Container<T>' does not define this operator or a conversion to a type acceptable to the predefined operator
2: error C2679: binary '=': no operator found which takes a right-hand operand of type 'Container<T>' (or there is no acceptable conversion)

I am not sure where I am going wrong here...

NYC Canuck
  • 345
  • 1
  • 4
  • 13
  • first of all = is assigment operator and you do not assign anything. 2. dereference this on return statement. 3. reallocate the storage.... 4. delete second for loop, etc. – joy Nov 27 '12 at 15:21
  • http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html – joy Nov 27 '12 at 15:23
  • @ecatmur - T *containerPtr; – NYC Canuck Nov 27 '12 at 15:25
  • @newgoegab - 2. done, 4 - removed - it was not meant to be there, was when I tried testing a different way. – NYC Canuck Nov 27 '12 at 15:26
  • @newgoegab - not sure what you mean by not assigning anything. I am copying the data from one array to the other, am I not? – NYC Canuck Nov 27 '12 at 15:27
  • Be cautious about using `=` to transfer data between your two containers. You might find this worth a read: http://stackoverflow.com/questions/3413470/what-is-stdmove – Rook Nov 27 '12 at 15:35

3 Answers3

3

This error

temp[i] = containerPtr[i];      // error 1 here

is most likely because you have not defined an operator[](size_t) to allow you to access elements of your container with square brackets. You need something like these const and non-const operators:

T& operator[](std::size_t index) { return containerPtr[index]; }
const T& operator[](std::size_t index) const { return containerPtr[index]; }

assuming containerPtr is some kind of dynamically sized array holding T objects. And this line:

*containerPtr = temp;               // error 2 here

is wrong. The type of *containerPtr is T, and you are trying to assign a Container<T> to a T. This is unlikely to work.

I suggest you use std::copy instead of the loops ans assignments, although I realise it might be against the spirit of your assignment.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks for the info. I thought that my error 2 was where the problem really laid. I fixed that and it works okay now. I gave the check to stonemetal as his answer was first. – NYC Canuck Nov 27 '12 at 16:46
1

2: error C2679: binary '=': no operator found which takes a right-hand operand of type 'Container' (or there is no acceptable conversion)

Is the more important error, it states that what ever you believe the types to be on

*containerPtr = temp;

this line of code is incorrect. It is impossible to assign a container<T> to a T value which is what you are trying to do. After you change temp to a pointer to a dynamically allocated array of T that is of the correct size(and remove the pointer deref on the assignment) the other error will go away on its own. Note you will also want to fix the memory leak where you forget to delete the previous array.

stonemetal
  • 6,111
  • 23
  • 25
  • I figured that was where the problem was. I have corrected and it works okay now. I still have to fix the memory leak, and then should be good. Thanks for the help! – NYC Canuck Nov 27 '12 at 16:47
0

Error 1: Pretty sure that you have to define operator[] for your Container class

Error 2: You should use following signature when overloading operator= :

template< typename T >
const T Container<T>::operator=(const Container<T>& rhs)
{ 
   ...
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
ABosyy
  • 11
  • 1