0

I think I just need another set of eyes to find out what I'm doing wrong.

This is the error:

bfgs_template.hpp:478:5: error: ‘di’ was not declared in this scope
bfgs_template.hpp:478:8: error: ‘b’ was not declared in this scope

and this is a snapshot of the code:

template<typename T>
void update_d();

template<typename T>
class BFGSB: public BFGS<T>{
protected:
  double *di;
  int b;
public:
  friend void update_d<T>();
};


template<typename T>
void update_d(){
    di[b] = 0.0; 
}

Btw. While I didn't post the rest of the code. di is initialized and if I make update_d a member of the class. Everything runs smooth.

Wilmer E. Henao
  • 4,094
  • 2
  • 31
  • 39
  • 1
    What kind of magic are you expecting the `friend` declaration to work? Pass a reference to an instance of `BFGSB` to `update_d` and it'll be able to operate on the private members of that instance. – Praetorian Apr 27 '13 at 21:59

1 Answers1

4

Just because update_d is a friend of BFGSB, it doesn't mean it can just access di and b. It's a non-member function just like any other. It isn't associated with any particular instance of BFGSB and so there are no particular di or b objects to access. Making it a friend simply means that it is allowed to access the di and b members of a BFGSB object. For example, it could do this:

template<typename T>
void update_d(){
    BFGSB<T> obj; // We can access the privates of this object
    obj.di[obj.b] = 0.0; 
}
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324