I'm attempting to read the int data
variable in parent class Foo
in child class Bar
.
The variable data
is initialized under a private:
and uses friend class Bar<T>
.
I am receiving the following compiler error:
$ g++ scopetest.cpp
scopetest.cpp: In constructor ‘Bar<T>::Bar()’:
scopetest.cpp:33:12: error: ‘data’ was not declared in this scope
cout << data;
^
I thought friend class Bar<T>
would make everything under private:
visible to Bar<T>
. But this doesn't seem to be the case.
Here is my code:
using namespace std;
template <typename T>
class Bar;
template <typename T>
class Foo{
private:
friend class Bar<T>;
int data;
public:
Foo() {
data = 0;
}
~Foo() {
}
};
template <class T>
class Bar : public Foo<T> {
public:
Bar<T>() : Foo<T>() {
cout << data;
}
~Bar() { }
};
int main(int argc, char* argv[]){
}
Is there anything obvious that I am doing wrong here?