0

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.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Jonathan Allen Grant
  • 3,408
  • 6
  • 30
  • 53

4 Answers4

3
KernelLock myLockArray[150];

Creates an array of 150 KernelLocks. You then then try to assign a KernelLock * to it. If you want pointers then you need to change your array to:

KernelLock* myLockArray[150];

If you don't want pointers then you just need to change your assignment to

myLockArray[initializedLocksCounter] = KernelLock(myAddrSpace, newLock);
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2
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>

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

You are fundamentally misunderstanding what an object is in C++. It's not like in Java.

Your 150 KernelLock objects are 150 KernelLock objects. Not pointers, and not references. And there are 150 of them. You cannot dynamically "expand" the array whenever you please, and doing so with new is dynamic allocation which requires managing pointers (KernelLock*).

From the sounds of it, you actually want a std::vector<KernelLock>.

Here are some resources for learning the basics of C++.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

you cannot use a value of type KernelLock* as element of an array of KernelLock's. You simply do:

myLockArray[i] = KernelLock(myAddrSpace, newLock);

where i is a valid index.

However, notice that by doing so you will pay the price of default-initializing 150 KernelLock's no matter what (and this might be expensive). It would be better to use the standard library:

std::vector<KernelLock> kernel_locks;
kernel_locks.reserve(some_size); // better to reserve if you have some sensible upper bound
kernel_locks.emplace_back(myAddrSpace, newLock);
Ilio Catallo
  • 3,152
  • 2
  • 22
  • 40