tags:

views:

118

answers:

3

In running Ubuntu and the g++ compiler I keep getting the same error from this code.

 myClass *arr;
 arr = new myClass*[myClassSize]; // line 24
 for(int a = 0;a<myClassSize;a++) 
   arr[a] = new myClass;

Here is the error:

cannot convert 'myClass **' to 'myClass *' in assignment 

The problem was on line 24.

+7  A: 

You need an extra * in the declaration of arr:

myClass** arr;

You seem to be trying to make an array of pointers, but type* is just a pointer to type / array of type.

Andrew Medico
+1  A: 

You're declaring arr as a pointer to a myClass. arr[a] is a dereference; it's the same as *(arr+a) which is a reference to myClass, not a pointer.

Mark Ransom
A: 

It says what it means you are trying to assign myClass ** to myClass * which isn't allowed without a cast. From the looks of it the line of code in question should read

arr = new myClass[myClassSize];//note the lack of star

That would create an array of myClass objects.

There is a certain amount of ambiguity between arrays and pointers in c++ so in this case the array returned by new is already capable of being assigned to a pointer.

if you want to make a array of pointers then you need to double the star on the def of arr to make a pointer to pointers of myclass.

stonemetal
No, the questioner is trying to put myClass* (the result of 'new myClass') into the array in the loop, so it's much more likely that myClass\** is what's needed.
Andrew Medico