473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help: derived class uses base class protect value

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

Jul 24 '06 #1
5 1375
In article <11**********************@m79g2000cwm.googlegroups .com>,
"tomy" <to******@gmail.comwrote:
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?
Jul 24 '06 #2

Daniel T. wrote:
>
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<intfoo;
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

Jul 24 '06 #3
tomy wrote:
>
Daniel T. wrote:
>>
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;}
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<intfoo;
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
Jul 24 '06 #4
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

Kai-Uwe Bux wrote:
Try

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

or

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

BTW: the inline is redundant.

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

Kai-Uwe Bux
Jul 24 '06 #5
tomy <to******@gmail.comwrote:
Kai-Uwe Bux wrote:
>Try

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

or

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

BTW: the inline is redundant.

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

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.
Tomy,
Please don't top-post. Your replies belong below or interleaved with
the original quoted message, with irrelevant context snipped.

Anyway, for a little more info, look in the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-35.18

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.