Job:

my class "network" contains a vector "taus". I wish "network" to be able to find the indexes of the n smallest elements.
My first idea was along the lines (this is mostly meant as pseudo-code):

#include<algorithm>
#include<vector>
#include<cstdlib>

class network
{
vector <double> taus;
vector <int> nsmallest;
vector <int> Indexes;
network(){
Indexes.resize(100);
taus.resize(100);
for(int i=0;i<100;i++){Indexes[i]=i; taus[i]=rand;}
}

bool tauComp(int i,int j) {return (taus[i]>taus[j]);};

void findSmallest(){
partial_sort_copy(Indexes.begin(), Indexes.end(), nIndexes.begin(), nIndexes.end(), &((*this).tauComp)); }
};

however, "ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&network::tauComp’" - which is what g++ responds.
So, since I am only bothering with one network at a time, I could make both tauComp and taus static, which should allow me to pass a pointer to it. However, that is pretty unsatisfactory, and I'd rather not have to worry about that whenever I fool around with several networks (which I guess I'm bound to be sooner or later).
I wouldn't mind making just tauComp static, but then I'd need some way of passing a pointer to tauComp to tell it which taus I want it to use when comparing (and I don't see how I can include that in the code above).
Then of course there's also the option of abandong partial_sort_copy and just do my own search algorithm, but that feels like giving up (and it's potentially a hazzle).

any suggestions? if there's an alternative to partial_sort_copy which could take taus directly, that would work as well...

I hope the above is clear =)

On reading this I thought that the obvious solution is to use a functional.
The functional can be defined as a member of your class, or not.

Additionally, I wasn't actually sure what algorithm you were implementing, in this example I consider that you have an vector of values, and wish to find the positions of the lowest values without disturbing the vector.

consider if you did this:

class comp
{
  const std::vector<double>& RefVec;   

  comp(const std::vector<double>& A) : Ref(A) {}

  bool operator()(const int A,const int B) const
  {
    return (RefVec[A]<RefVec[B]);
  }
};

int main()
{
  std::vector<int> Index;
  std::vector<int> Out;
  std::vector<double> Value;
  
  for(int i=0;i<10;i++)
    {
      Value.push_back( ((i % 3)-1.0) * i*i);
      Index.push_back(10-i);
    }

  Out.resize(3);
  partial_sort_copy(Index.begin(),Index.end(),Out.begin(),
		    Out.end(),comp(Value));
 
  for(int i=0;i<3;i++)
    {
      std::cout<<"Lowest Values == "<<Value[Out[i]]<<std::endl;
    }
}

The function idea can be extended to include very complex ideas, since the functional has state.

Next: You can use member pointers to functions, but you need a calling function:
You just need a slightly different syntax. However, it wont help you (easily) here since you are going to have to do a lot of bind2nd etc to get it to work [or use boost::bind].

Note the comment the compiler gives you is correct, the key phase is address of an unqualified which is because you missed out the className:: part of the qualification.

class X
{
  public:
  // Two functions with same signature [ void () const ]
  void call() const { std::cout<<"CALL"<<std::endl; }
  void call2() const { std::cout<<"Call 2 "<<std::endl; } 
};


void delegate(const X* T,void (X::*fx)() const)
{
  (T->*fx)();
}

int main()
{
   X Item;
   delegate(&Item,X::call);
   delegate(&Item,X::call2);
}

Hope that helps.

It seems like you understood well enough what algorithm it was =) I think it was the functor solution that I was fishing for.

I'm a bit intrigued by the second bit of code. I guess I'm not fully up to speed with what the "->" operator does. What it's called? I'd like to google it. Would the above allow me to send Item.call as a pointer?

thanks

This question has already been answered. Start a new discussion instead.