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):


class network
vector <double> taus;
vector <int> nsmallest;
vector <int> Indexes;
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 =)

Recommended Answers

All 3 Replies

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);

  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
  // 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)

int main()
   X Item;

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 as a pointer?


There is a nice tutorial at

The ->* is a pointer dereference to a function pointer. Easiest way to google it is
to put it in quotes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.