473,324 Members | 2,124 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,324 software developers and data experts.

how do I access a member class of a class template from a template child class

Hi all,
I got the following problem. I have two class templates, one inheriting from the other.

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. struct M{
  3.     class A{};
  4. };
  5.  
  6. template<typename T>
  7. class B:public M<T>{
  8.     A a;
  9. };
I know that this code is not correct. I've read about the compiler's two-phase lookup of members in template classes. So I assume that giving the fully qualified name would work. But it doesn't: If I replace the
Expand|Select|Wrap|Line Numbers
  1. A a;
with
Expand|Select|Wrap|Line Numbers
  1. M<T>::A a;
the compiler (gcc 4.4.3) complains about this line with
Expand|Select|Wrap|Line Numbers
  1. error: ISO C++ forbids declaration of ‘A’ with no type
  2. error: expected ‘;’ before ‘a’
My questions are:
1. Is there any way to use the A class in my B class?
2. I have read that for non-type members of the class template M I can use the "using" declaration inside the child class to be able to use it like its own member. Is there anything I like this for type-members like class A?
I would gratefully appreciate any help.
~<><~~~~~~~ presencia
Jun 18 '10 #1

✓ answered by Banfa

The problem is that because M is a template class the compiler can not tell what type of thing M<T>::A is and assumes it is a symbol. You need to tell the compiler it is a type name like this

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class B: public M<T>{
  3.     typename M<T>::A a;
  4. };
  5.  

2 1919
Banfa
9,065 Expert Mod 8TB
The problem is that because M is a template class the compiler can not tell what type of thing M<T>::A is and assumes it is a symbol. You need to tell the compiler it is a type name like this

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class B: public M<T>{
  3.     typename M<T>::A a;
  4. };
  5.  
Jun 18 '10 #2
Thanks Banfa,
I don't know why I didn't try just this before. It works, thank you. This thread is solved.
~<><~~~~~~~ presencia
Jun 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lkrubner | last post by:
My code was dying on the line below where I use method_exists: if (class_exists($nameOfClassToBeUsed)) { $object = new $nameOfClassToBeUsed(); $this->arrayOfAllTheObjectsSoFarLoaded = &...
2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
9
by: Ian | last post by:
Can it be done? If so, what's the syntax. For example a full specialisation, template <typename T> struct X { template <typename C> void foo( C a ) {} };
5
by: owais | last post by:
Hi, I have a problem, I want to implements Parent class interface methods in child class. for e.g -------------- Test1.vb ---------------- Imports System Imports System.Web.UI Imports...
3
by: danilo.horta | last post by:
Hi guys I'm trying to accomplish a slightly difficult task. I think it's more easy to explain trought an unworking code: template<class T, size_t numDim> VecBasis {/*...*/}; typedef...
2
by: montyshasta | last post by:
Take this code as a base case, it compiles successfully: struct R { int i; }; class S : public R { void F(void) {i = 0;} };
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
2
by: Korosov | last post by:
Hi! GCC (4.1.2 20070502 (Red Hat 4.1.2-12)) gives an error: test.cpp: In constructor <Dclass<T>::Dclass()>: test.cpp:27: error: <a> was not declared in this scope when compiling the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.