473,387 Members | 1,798 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,387 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 2262
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.