My program contains a class, here's its simplified form:
Units.h:
#define fullwidth 200
#define fullheight 200
class Units
{
public:
[...]
void MovingUp(Units* Detector[fullwidth][fullheight], Units Self);
[...]
}
It defines a function that takes two arguments as input: an array of object pointers for "Units" objects, and a special chosen "Units" object.
Here's the main part:
#include "Units.h"
[...]
int i,j;
Units* DetectorField[fullwidth][fullheight];
Units Examples[20];
for (j=0;j<fullheight;j++)
{
for (i=0;i<fullwidth;i++)
{
DetectorField[i][j] = NULL;
}
}
It creates the array of "Units" object pointers that is necessary for the function in "Units" and sets all those values to NULL at the very beginning.
After that, I try to call the function of the object through one randomly chosen pointer. Obviously, if the pointer is referenced to NULL, the call is impossible, but Visual C++ shows error even before I run the program.
DetectorField[12][12]->MovingUp(DetectorField,DetectorField[12][12]);
For this version, Visual C++ writes after compiling: cannot convert parameter 2 from 'Units *' to 'Units' - of course, since DetectorField
itself is an array of pointers. Then I try to reference to the object it points, this way:
DetectorField[12][12]->MovingUp(DetectorField,&DetectorField[12][12]);
MSVC writes after compiling: cannot convert parameter 2 from 'Units **' to 'Units'
There isn't clear for me, why the second version reference to the pointer of an object pointer, not to an object. Also, how should I write the function call on the right way?
For this call to be valid:
DetectorField[12][12]->MovingUp(DetectorField, DetectorField[12][12]);
The second argument has to be a Unit *
type like:
class Unit
{
// ...
void MovingUp(Units* Detector[fullwidth][fullheight], Units *Self);
};
If you want to keep Unit::MovingUp
as is then change your call to:
DetectorField[12][12]->MovingUp(DetectorField, *DetectorField[12][12]);
Note you're passing Unit in by value here. If you have other classes inheriting from Unit you risk object slicing. I suggest at least changing it to take a Unit &
reference.
"There isn't clear for me, why the second version reference to the pointer of an object pointer, not to an object."
When &
apears this way, it mean the address of
.
"Also, how should I write the function call on the right way?"
What you should do either call it that way:
DetectorField[12][12]->MovingUp(DetectorField,*DetectorField[12][12]);
^
Which means, the value pointed by DetectorField[12][12] (since it's a pointers array).
Or change the funciton decleration to:
void MovingUp(Units* Detector[fullwidth][fullheight], Units* Self);
^
Which means this function will recive a pointer to units.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With