help: derived class uses base class protect value

T

tomy

Hi Group:
I just come to a confusion thing which is different from what my text
book says.
The code is below:

//************************************
class Base
{
protected:
int mutex;
};

class Foo : public Base
{
public:
inline void bar(int v) { mutex = v;}
};
//***************************************
compiled result is:

foo.h: In function `void bar()':
foo.h:33: error: `mutex' undeclared (first use this function)
foo.h:33: error: (Each undeclared identifier is reported only once for
each function it appears in.)
//***************************************
To my surprise, the code won't be compiled. The compiler(gcc) says the
variable mutex didn't be declared. However the text book told me the
derived class could see the Base class' protected thing through public
derived, didn't it?

could someone give me any suggestion?


--tomy
 
D

Daniel T.

"tomy said:
Hi Group:
I just come to a confusion thing which is different from what my text
book says.
The code is below:

//************************************
class Base
{
protected:
int mutex;
};

class Foo : public Base
{
public:
inline void bar(int v) { mutex = v;}
};
//***************************************
compiled result is:

foo.h: In function `void bar()':
foo.h:33: error: `mutex' undeclared (first use this function)
foo.h:33: error: (Each undeclared identifier is reported only once for
each function it appears in.)
//***************************************
To my surprise, the code won't be compiled. The compiler(gcc) says the
variable mutex didn't be declared. However the text book told me the
derived class could see the Base class' protected thing through public
derived, didn't it?

could someone give me any suggestion?

I pasted the code above into my gcc complier and it compiled just fine.
Are you sure that the code above is what you have?
 
T

tomy

Daniel said:
I pasted the code above into my gcc complier and it compiled just fine.
Are you sure that the code above is what you have?

Thanks for your post.
Sorry, I tried again the code above which actually do work. :)

The original code is with template. And as below :

//******************************************************
template<class T>
class Base
{
protected:
int mutex;
};

template<class T>
class Foo : public Base<T>
{
public:
inline void bar(int v) { mutex = v;}
};

//****************test main***********
int main()
{
Foo<int> foo;
foo.bar(100);
return 0;
}
//****************************************************
compiled result:

foo.cpp: In member function `void Foo<T>::bar(int)':
foo.cpp:13: error: `mutex' undeclared (first use this function)
foo.cpp:13: error: (Each undeclared identifier is reported only once
for each function it appears in.)
//*****************************************************

I wonder what happens with the TEMPLATE?

--tomy
 
K

Kai-Uwe Bux

tomy said:
Thanks for your post.
Sorry, I tried again the code above which actually do work. :)

The original code is with template. And as below :

//******************************************************
template<class T>
class Base
{
protected:
int mutex;
};

template<class T>
class Foo : public Base<T>
{
public:
inline void bar(int v) { mutex = v;}

Try

inline void bar(int v) { this->mutex = v;}

or

inline void bar(int v) { Base<T>::mutex = v;}

BTW: the inline is redundant.
};

//****************test main***********
int main()
{
Foo<int> foo;
foo.bar(100);
return 0;
}
//****************************************************
compiled result:

foo.cpp: In member function `void Foo<T>::bar(int)':
foo.cpp:13: error: `mutex' undeclared (first use this function)
foo.cpp:13: error: (Each undeclared identifier is reported only once
for each function it appears in.)
//*****************************************************

I wonder what happens with the TEMPLATE?

Templates is where some strange name-lookup issues kick in.


Best

Kai-Uwe Bux
 
T

tomy

Great, your proposal really do good job. Both of them work fine.
--"Templates is where some strange name-lookup issues kick in" That's
the key.

Many Thanks.

--tomy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top