Search code examples
c++arraysoperator-keywordnachos

C++ no match for 'operator=' in my custom object array


I have a class called KernelLock, and I am creating an array of KernelLocks called myLockArray. I declare it like this: KernelLock myLockArray[150];

When I try to add a new KernelLock to myLockArray, I get the aforementioned error. Here is the exact line I get the error on:

myLockArray[initializedLocksCounter] = new KernelLock(myAddrSpace, newLock);

and here is the exact error:

error: no match for 'operator=' in 'myLockArray[initializedLocksCounter] = (((KernelLock*)operator new(8u)), (<anonymous>->KernelLock::KernelLock(myAddrSpace, newLock), <anonymous>))

In case it helps, I am compiling with gcc through Nachos.


Solution

  • KernelLock myLockArray[150];
    

    This is an array that contains 150 kernel locks.

    When I try to add a new KernelLock to myLockArray

    You cannot add any objects to an array. The size of an array is always constant. It always has 150 locks.

    myLockArray[initializedLocksCounter] = new KernelLock(myAddrSpace, newLock);
    

    This is wrong. new returns the address of a dynamically allocated object. You're trying to assign that address to the existing KernelLock object at the index initializedLocksCounter. You cannot assign an address to a non-pointer object (unless the object has corresponding (non-explicit) constructor).

    If you want a growing array, use std::vector<KernelLock>