5

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?

4
  • 2
    DetectorField[12][12] is a pointer. So &DetectorField[12][12] is a reference to a pointer. To get the object, you dereference the pointer using *. Try DetectorField[12][12]->MovingUp(DetectorField, *DetectorField[12][12]); (I'm just answering that point and not paying attention to questions like has memory been allocated, why not use smart pointers, vectors, etc...) Commented Jul 20, 2013 at 0:22
  • I'm kinda amateur at using pointers. Basic things are OK for me, but pointers and the lot of memory-handling-related operations are a bit too much for my brain yet. I hope I can learn. Thanks! Commented Jul 20, 2013 at 0:26
  • 1
    BTW - +1 for being very clear what you don't understand and what is your question. Commented Jul 20, 2013 at 0:31
  • Well, I was banned from asking for a very long time, so I thought it over twice before I asked. Strict expectations, quality questions. Commented Jul 20, 2013 at 0:35

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, "Units" already has inherited classes, thanks for mentioning it! Can you explain, what should I change? void MovingUp(Units& Detector instead of void MovingUp(Units* Detector?
1

"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.

1 Comment

Well, it totally makes sense - I just didn't know that this is a valid operation, since I haven't seen using * operator this way. Thank you!
1

The type of an element in your array is Units*. You declared it that way: Units* Detector[fullwidth][fullheight] - a two-dimensional array of Units*. The address of a Units* is type Units**.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.