473,224 Members | 1,977 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,224 software developers and data experts.

Templated classes with nested classes

card
10
Hi everyone,

I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes:
Expand|Select|Wrap|Line Numbers
  1. #include <stack>
  2.  
  3. template <class T>
  4. class A
  5. {
  6. public:
  7.  
  8.     // constructor
  9.     A(const T &zz): z(zz) {}
  10.     T z;
  11.  
  12.     // nested class
  13.     class B
  14.     {
  15.     public:
  16.         // constructor
  17.         B(const T &xx, const T &yy): x(xx), y(yy) {}
  18.         T x;
  19.         T y;
  20.     };
  21. };
  22.  
  23. template <class T>
  24. class Iter
  25. {
  26. public:
  27.     Iter(const T &xx): x(xx) {}
  28.     T x;
  29.  
  30.     std::stack< A<T> > aStack;
  31.     std::stack< A<T>::B > bStack;
  32. };
  33.  
  34.  
As you can see, class A is a templated class and class B is nested within A. I have another templated class called Iter. Within Iter, I have two stacks. One contains objects of A and the other objects of the nested class B contained in A. A simple main program to build the code is below:

Expand|Select|Wrap|Line Numbers
  1. #include "nested.H"
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.     A<int> myAObject(5);
  6.     A<int>::B myBOjbect(6,7);
  7.     Iter<int> myIterObject(2);
  8.  
  9.     return 0;
  10. }
  11.  
The build with g++ fails with the following error:

nested.H:44: error: type/value mismatch at argument 1 in template parameter list for ‘template<cla\
ss _Tp, class _Sequence> class std::stack’
nested.H:44: error: expected a type, got ‘A<T>::B’
nested.H:44: error: template argument 2 is invalid

As you can see, it doesn't know what to do with A<T>::B in the stack declaration of the templated class Iter. How do I reference this nested class within Iter? Obviously, I can do it in main as shown above. Many thanks.

-David
Oct 19 '08 #1
2 2256
boxfish
469 Expert 256MB
Try changing
std::stack< A<T>::B > bStack;
to
std::stack< typename A<T>::B > bStack;
Somehow that lets the compiler know A<T>::B is a type, but I have no idea why you have to do it. Template syntax-
AAAAAAAAAAAAARRRRRRRRRRGGGH!!
Good luck.
Oct 19 '08 #2
card
10
Try changing
std::stack< A<T>::B > bStack;
to
std::stack< typename A<T>::B > bStack;
Somehow that lets the compiler know A<T>::B is a type, but I have no idea why you have to do it. Template syntax-
AAAAAAAAAAAAARRRRRRRRRRGGGH!!
Good luck.
Thanks boxfish! I've been pulling my hair out for a couple hours. That did the trick.
Oct 19 '08 #3

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Eddie Parker | last post by:
Hi! I'm having an interesting problem that I can't seem to get to work, and I'm curious if someone could either a) tell me how to make it work, or b) tell me why it *can't* work. :) Anyhow,...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
2
by: mattjgalloway | last post by:
I'm having some problems with a templated member function of a templated class. Unfortunately I can't replicate it with a simple example so I know something odd must be going on!!! Basically it's...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.