A brief outline: I have a base class that builds some data objects. I then have a child class that inherits all the public methods and pointers to objects from the base class.
In that child class, I want to construct a functor (as a struct) to use in a for_each loop. My problem is that in the functor operator, I get an error when trying to access o开发者_如何学Pythonbjects.
Abbreviated example:
class Child : public BaseClass {
Child(DataSource& in_data): Base(in_data){};
struct foo {
double operator() (int x){
double y = in_data.some_function(x);
// do stuff
}
};
}
error: not able to access in_data.some_function.
Ideas?
Nested classes don't have visibility of members of their enclosing class (they're like static
nested classes in Java).
If you want foo
to access in_data
, you will need to provide it a reference explicitly. So in foo
's constructor, either have it take a reference to a DataSource
, or a reference to a Child
(and pass *this
).
Your foo
doesn't know about DataSource
.
consider this
struct foo
{
DataSource& ds;
foo(DataSource& a) :ds(a) {}
double operator() (int x)
{
double y = ds.in_data.some_function(x);
.....
}
};
You have to create foo by giving it a DataSource to refer to.
精彩评论