0

I'm working in a class function:

void Planner::CellSort()
{
    vector<vector<int>> *pointer_to_open = &openlist;
    sort(pointer_to_open->begin(), pointer_to_open->end(), Compare);
}

When I try to compile it I get the following error:

error: must use '.*' or '->*' to call pointer-to-member function

But When I try the following compile just fine:

sort((&openlist)->begin(), (&openlist)->end(), Compare);

Isn't both actually doing the same? or what's the difference between those two approaches.

This is my Compare function:

bool Planner::Compare(const vector<int> a, const vector<int> b)
{
    int f1 = a[2] + a[3]; // f1 = g1 + h1
    int f2 = b[2] + b[3]; // f2 = g2 + h2
    return f1 > f2;
}

Thank you.

Steven Daniel Anderson
  • 1,435
  • 2
  • 11
  • 17
  • can you show the compare function? if the compare function parameters something along the line of const std::vector& a, const std::vector& b, it should work. – Yucel_K May 04 '19 at 06:31
  • https://ideone.com/Rw93A8 demonstrates how its down using sort(pointer_to_open->begin(), pointer_to_open->end(), Compare); – Yucel_K May 04 '19 at 06:53
  • also, don't understand why this question marked duplicate with a link to a potential answer. 0.0'. it seems the issue at hand different then whether if a function () has higher precedence then the binding operator... – Yucel_K May 04 '19 at 07:07
  • @Yucel_K I added the compare function, I check the code it the URL and it seems like mine I still can't figure out why it doesn't compile. – Steven Daniel Anderson May 04 '19 at 17:26
  • when you call the compare function, the vectors are passed as copies. instead, they could be passed by reference. that way you don't make a copy of the vectors every time you call the compare function. so you gain a speed advantage. all you have to do is change compare function. as such; **Compare(const vector& a, const vector& b)** and it should work – Yucel_K May 05 '19 at 11:24
  • continued: Notice the **&** operator. this basically telling the compiler that compare function is taking the address of the vector as a parameter rather than a copy of the vector. – Yucel_K May 05 '19 at 11:32

1 Answers1

1

Consider vector<vector<int>> *pointer_to_open = &openlist;

So, here pointer_to_open has the address of openlist. So, if do *pointer_to_open, it will point to the value of openlist, not pointer_to_open (as it has address of openlist).

You are using:

pointer_to_open and not *pointer_to_open

Consider this and this article on pointers and vectors.

What you can do:

void Planner::CellSort()
{
    vector<vector<int>> *pointer_to_open = &openlist;
    sort(*pointer_to_open->begin(), *pointer_to_open->end(), Compare);
}

It should work as fine as using &openlist

R4444
  • 2,016
  • 2
  • 19
  • 30