0

I have a base class and the derived class. I need to access the protected member of the base class in the derived class. However, Eclipse does not allow me to access the data member as if it were a member of a derived class without caring that it was inherited. How do I do that?

class BaseClass {
protected:
static int a;
int b;
}


class DerivedClass: public BaseClass {    
void SomeMethod {    
a=10; // cannot resolve symbol
b=10; // cannot resolve symbol
BaseClass::a=10; //does not complain
BaseClass::b=10; //does not complain    
}
}
David Kiger
  • 1,958
  • 1
  • 16
  • 25
user592748
  • 1,194
  • 3
  • 21
  • 45
  • 1
    As good practice, may I suggest using 'this-> b' – Bingo Feb 24 '13 at 22:20
  • Are these actual compiler errors or are these syntactic/semantic error highlights provided by the eclipse IDE? – Rich Feb 24 '13 at 22:23
  • When I use this->, only the data members of the DerivedClass show up in the auto complete. I cannot see any data members of the Base Class – user592748 Feb 24 '13 at 22:25
  • 1
    @Bingo: That's debatable. – Lightness Races in Orbit Feb 24 '13 at 22:35
  • @Bingo Thanks, using `this->` worked. It was class inheritance with the base class using templates. Hence, I guess this is needed [here](http://stackoverflow.com/questions/7076169/not-declared-in-this-scope-error-with-templates-and-inheritance) – user592748 Feb 24 '13 at 23:47

1 Answers1

3

I couldn't completely understand your question, but fixing the syntax errors, the following should work:

class BaseClass {
protected:
static int a;
int b;
}; // <-- Missing semicolon

int BaseClass::a = 0; // Define static member

class DerivedClass: public BaseClass {    
void SomeMethod() { // <-- Missing ()
a=10;
b=10; 
}
};// <-- Missing semicolon
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • Yes, even with the correct syntax, it does not work. Are you also using Eclipse CDT? – user592748 Feb 24 '13 at 22:24
  • No, not currently. Is your question about autocomplete in Eclipse CDT? – Jesse Good Feb 24 '13 at 22:31
  • 1
    @user592748: What do you mean "it does not work"? Does it compile? Or is your editor just complaining? – johnsyweb Feb 24 '13 at 22:32
  • Actually, I was trying to simplify the code for the question. In the process left out a crucial detail. It was inheritance of classes using templates. The problem was solved using `this->`. It is something to do with 2-phase look up, that I learnt from this [question](http://stackoverflow.com/questions/7076169/not-declared-in-this-scope-error-with-templates-and-inheritance) – user592748 Feb 24 '13 at 23:41