0

I have a int costMap[20][20]; inside a dialog class.

Inside that class I am making an object from another class called pathFind

int costMap[20][20];
//costMap is filled with values (that represent the cost for each node -> pathFinder)
int (*firstElement)[20] = costMap;
pathFind *finder = new pathFind(*firstElement);

I would like to be able to acces the values of the multidimensional array inside the pathFind class

int (*costMap)[20];

pathFind::pathFind(int *map){
    costMap = ↦
}

However this doesn't seem to work. I get an "cannot convert int** to int(*)[20] error.

Question: How can you access the elements of a multidimensional array through a pointer in another class

3
  • 1
    Class names may not have . in them. Commented Jan 5, 2013 at 20:31
  • What line gives the error? Commented Jan 5, 2013 at 20:32
  • @LightnessRacesinOrbit: :p Commented Jan 5, 2013 at 20:34

3 Answers 3

1

You should do this:

pathFind::pathFind(int (*map)[20] ){
    costMap = map;
}

That is, match the types!

Also note that T (*)[N] and T** are not compatible types. One cannot convert to other. In your code, you're trying to do that, which is what the error message tells you.

Besides, there are other issues with your code, such as you should avoid using new and raw-pointers. Use std::vector or other containers from the Standard library, and when you need pointer, prefer using std::unique_ptr or std::shared_ptr whichever suits your need.

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

1 Comment

It's not really an also. It's the same thing!
1

You would have to write

pathFind::pathFind(int (*map)[20]){ ... }

but this being C++ you may find this better:

template< std::size_t N, std::size_t M >
pathFind::pathFind(int (&map)[N][M]){ ... }

1 Comment

@Lightness Races in Orbit: True, thank you. That goes to show I would never do something like that
1
pathFind::pathFind(int *map){

This expects a pointer to an integer.

Elsewhere, you've already discovered the correct type to use:

pathFind::pathFind(int (*map)[20]){

Try to avoid this pointer hackery, though.

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.