-3

I am building a class Boggle in C++. In the Boggle class, I have declared a structure type called boardIndex:

struct Boggle::boardIndex {
    int row, col;
};

And a callback function to compare two "boardIndex"s:

int Boggle::CmpByIndex(boardIndex a, boardIndex b)

I want to pass the callback function to a Set of boardIndex elements within the Boggle.cpp file:

Set<boardIndex> usedIndices(CmpByIndex); 

Is this possible? In the current form, I'll get an error that a "reference to non-static member function must be called." I don't have any objects of the Boggle class - is there another way of calling the CmpByIndex function here?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
whitehall
  • 77
  • 8
  • 1
    Please show actual code, not your interpretation of it. What you show is not compilable at all. – Slava Mar 08 '17 at 22:26
  • Try to declare `int Boggle::CmpByIndex(boardIndex a, boardIndex b)` as a friend function – Fureeish Mar 08 '17 at 22:29
  • If you don't have objects of the class, all your member functions should be static. – Barmar Mar 08 '17 at 22:29
  • Member functions needs an *object* to be called on. Have you tried making the function `static` (which is hinted at by the error message)? – Some programmer dude Mar 08 '17 at 22:29
  • @someprogrammerdude the **title** precisely asks how to do it *without* making a function static, so suggesting to make it `static` makes no sense – Fureeish Mar 08 '17 at 22:30
  • 1
    @Fureeish But it seems like his mistake is in making the function non-static in the first place. – Barmar Mar 08 '17 at 22:31
  • @Barmar but should we assume that? I'm pretty sure that declaring that function as a `friend` will solve the problem **and** it won't become `static` – Fureeish Mar 08 '17 at 22:32
  • @Fureeish If it's not static, you need to call the method through an object. What object will it be called through? – Barmar Mar 08 '17 at 22:34
  • 1
    @Fureeish Why should it be a `friend` function? Do you mean to turn the *member* function into a non-member function? Also, the title just says that the function is *currently* non-static, but I see no requirement that it must stay that way (even though it might be loosely implied). – Some programmer dude Mar 08 '17 at 22:35
  • @Barmar, if a function is declared as a friend, you can pass object as a parameter. However yes, I see my bad logic here, I pointed out the title and I also ignored the fact that `friend` makes a function non-member. My bad – Fureeish Mar 08 '17 at 22:37
  • @Fureeish being a friend of a class doens't allow you to pass an object of that class as a parameter, *all* functions can do that. Friendship gives you access to privates – Caleth Mar 08 '17 at 22:44
  • @caleth I do know that – Fureeish Mar 08 '17 at 22:49

1 Answers1

1

I don't have any objects of the Boggle class - is there another way of calling the CmpByIndex function here?

If you do not have any objects then you cannot call non-static member function. So you have 2 solutions:

  • make this function either static or non member of class Boogie
  • create or somehow obtain an instance of Boogle and bind it to call of this method
Slava
  • 43,454
  • 1
  • 47
  • 90