473,395 Members | 1,647 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.

What's wrong here? Accessing members of a templated base class

If anyone here can tell me what's going wrong in this snippet, I'd be much
obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...

template<typename T>
struct base
{
T m_x;
};

template<typename T>
struct derived : public base<T>
{
void bleh();
};

template<typename T>
void derived<T>::bleh()
{
T b = base<T>::m_x; // OK
T a = m_x; // error: `m_x' undeclared
}

int main()
{
derived<int> c;
c.bleh();
}

Jul 22 '05 #1
4 1262

"Pete" <yo**************@spamsrus.co.uk> wrote in message
news:As*******************@fe2.news.blueyonder.co. uk...
If anyone here can tell me what's going wrong in this snippet, I'd be much
obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...

template<typename T>
struct base
{
T m_x;
};

template<typename T>
struct derived : public base<T>
{
void bleh();
};

template<typename T>
void derived<T>::bleh()
{
T b = base<T>::m_x; // OK
T a = m_x; // error: `m_x' undeclared
}


It's because 3.4 implements two phase look up correctly and therefore does
not examine the base class when looking up a unqualified dependent name.

Either base<T>::m_x or this->m_x will fix the problem.

John
Jul 22 '05 #2
Pete wrote in news:As*******************@fe2.news.blueyonder.co. uk in
comp.lang.c++:
If anyone here can tell me what's going wrong in this snippet, I'd be
much obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4.
Thanks...


The name m_x from the base 'base< T >' is dependant on the template
argument 'T', you need to tell a conforming compiler (g++ 3.4 in this
case) that 'm_x' is in 'base< T >' otherwise it will assume its a global.

template<typename T>
struct base
{
T m_x;
};

template<typename T>
struct derived : public base<T>
{
Fix 1:

using base< T >::m_x;
void bleh();
};

template<typename T>
void derived<T>::bleh()
{
Fix 2:
T b = base<T>::m_x; // OK
T a = m_x; // error: `m_x' undeclared
Fix 3:

T c = this->m_x;
}


Note that Fix 1 puts a requirement on derived< T > that the
instansiated specialization for 'base< T >' contains a non-type
member m_x, Fix 2 and 3 only make this requirement if
derived< T >::bleh() is instantiated.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

"Pete" <yo**************@spamsrus.co.uk> wrote in message
news:As*******************@fe2.news.blueyonder.co. uk...
If anyone here can tell me what's going wrong in this snippet, I'd be much
obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...

template<typename T>
struct base
{
T m_x;
};

template<typename T>
struct derived : public base<T>
{
void bleh();
};

template<typename T>
void derived<T>::bleh()
{
T b = base<T>::m_x; // OK
T a = m_x; // error: `m_x' undeclared
}

int main()
{
derived<int> c;
c.bleh();
}


That's two phase name lookup. I just checked our FAQ and saw that it is
actually mentioned there.
http://www.parashift.com/c++-faq-lit....html#faq-34.1
7
If you want to know even better then read Part II (Chapters 9 and 10) of C++
Templates (Josuttis/Vandevoorde). They explain it quite well in their book.

-Sharad


Jul 22 '05 #4
Sharad Kala wrote:
That's two phase name lookup. I just checked our FAQ and saw that it is
actually mentioned there.
http://www.parashift.com/c++-faq-lit....html#faq-34.1 7
If you want to know even better then read Part II (Chapters 9 and 10) of
C++ Templates (Josuttis/Vandevoorde). They explain it quite well in their
book.


Ah, the c++-faq. Nice explanation... quite an eye-opener. I'll have to
change a load of code because of that one. Bah.

Thanks for the help, everyone.
Jul 22 '05 #5

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

Similar topics

1
by: Alexandre Tolmos | last post by:
Hi all, I can't compile the following code with Gcc 3.3 (compiles with CodeWarrior 8): template <typename T = int> class Boule { friend class Boule<int>; friend class Boule<float>;
25
by: John Harrison | last post by:
This code fails to compile on Comeau C++ and VC++ 7.1 (with language extensions disabled) template <class T> struct B { T b; }; template <class T>
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
5
by: Martin Jřrgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
4
by: Xavier Décoret | last post by:
I have just come across the following fact. Names defined in a template superclass of the current template must be qualified as being from the superclass. Alternatively, you can also qualify...
17
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie. int, double etc. (something along the lines of...
1
by: cpunerd | last post by:
Hello, I'm not new to C++, but for some reason, until now I'd never had a need for deriving templated classes. Now though, I find myself seeing a weird problem. If I have a templated base class...
5
by: Jeff | last post by:
ASP.NET 2.0 This code crashes. It generate this error: Value cannot be null. Parameter name: type I've created some custom web.config settings and this crash is related to accessing theme...
12
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.