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

Using nested class, interface, inheritance gives error

5
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The functions in the interfaces are defined in book, while the nested-class part is added by me. The reason is that if I don't declare a nested class, say Position, how could it be passed as arguments to those defined functions? However, it seems that the compiler doesn't recognize the nested class Position above one level in class hierarchy. The error occurs in the last few lines of the code.

Could you please suggest how to make it work. I really appreciate.
==========================================
The error messages generated by gcc 3.4.4:

Interfaces.h:38: error: expected `,' or `...' before '&' token
Interfaces.h:38: error: ISO C++ forbids declaration of `Position' with no type
Interfaces.h:39: error: expected `,' or `...' before '&' token
Interfaces.h:39: error: ISO C++ forbids declaration of `Position' with no type

==========================================
Expand|Select|Wrap|Line Numbers
  1. //interfaces.h
  2.  
  3. #ifndef INTERFACES_H_
  4. #define INTERFACES_H_
  5. #include "RuntimeException.h"
  6. template <typename Object>
  7. class InspectableContainer {
  8. public:
  9.   virtual int size() const = 0;
  10.   virtual bool isEmpty() const = 0;
  11.   class ObjectIterator;
  12.   virtual ObjectIterator elements() const;
  13.   class EmptyContainerException : public RuntimeException {  
  14.   public:
  15.     EmptyContainerException(const string& err) : EmptyContainerException(err) {}
  16.   };
  17.   class BoundaryViolationException : public RuntimeException {  
  18.   public:
  19.     BoundaryViolationException(const string& err) : RuntimeException(err) {}
  20.   };
  21. };
  22.  
  23. template <typename Object>
  24. class InspectablePositionalContainer
  25.   : public InspectableContainer<Object> {
  26. public:
  27.   class Position;                    // node position type
  28.   class PositionIterator;                // position iterator
  29.   virtual PositionIterator positions() const;            // get position iterator
  30.   class InvalidPositionException : public RuntimeException {  
  31.   public:
  32.     InvalidPositionException(const string& err) : RuntimeException(err) {}
  33.   };
  34. };
  35.  
  36. template <typename Object>
  37. class PositionalContainer
  38.   : public InspectablePositionalContainer<Object> {
  39. public:
  40.   virtual void swapElements(const Position& v, const Position& w) = 0; //ERROR OCCURS HERE!!!!!!!!!!!!
  41.   virtual Object& replaceElement(const Position& v, const Object& e) = 0; //ERROR OCCURS HERE!!!!!!!!!!!!
  42. };
  43. #endif
  44.  
Feb 16 '07 #1
4 3004
rach
5
It turned out that the problem does not lie in nested classes nor interfaces. If I remove
template <typename Object>
and correct one typo in a line, then it compiles.

typo in line 13:
replce
EmptyContainerException(const string& err) : EmptyContainerException(err) by
EmptyContainerException(const string& err) : RuntimeException(err)

I've seen articles that compare template with inheritance. I wonder why I cannot use templates with inheritance relation, that is, replacing class hierarchy by template hierarchy.
Feb 16 '07 #2
Ganon11
3,652 Expert 2GB
Glad you figured it out - I'm sorry to say that when I looked at your code, I didn't have a clue as to how to help, so it's good you figured it out on your own.
Feb 16 '07 #3
rach
5
Thank you for looking at my complicated code. (I should have simplified it first.)

Here I summarize my findings.

illegal:
[HTML]
template <typename T>
class A {
public:
typedef int myInt; //member type
class myClass; //member class
virtual void f();
};

/* B inherits from A */
template <typename T>
class B : public A<T> {
public:

virtual void g (const myInt& i, const T& t);
//ERROR - myInt undeclared

virtual void h (const myClass& m);
//ERROR - myClass undeclared
};

/* Concrete implementation of interface A and B */
template <typename T>
class C : public B<T> {
public:

void f() { /* implementation */ }

void g(const myInt& i, const T& t) { /* implementation */ }
//ERROR - myInt undeclared

void h(const myClass& m) { /* implementation */ }
//ERROR - myClass undeclared
};
[/HTML]

error message:


test.h:14: error: expected `,' or `...' before '&' token
test.h:14: error: ISO C++ forbids declaration of `myInt' with no type
test.h:17: error: expected `,' or `...' before '&' token
test.h:17: error: ISO C++ forbids declaration of `myClass' with no type
test.h:28: error: expected `,' or `...' before '&' token
test.h:28: error: ISO C++ forbids declaration of `myInt' with no type
test.h:31: error: expected `,' or `...' before '&' token
test.h:31: error: ISO C++ forbids declaration of `myClass' with no type


After removing template <typename T> and occurrence of T, it becomes legal.

[HTML]
class A {
public:
typedef int myInt; //member type
class myClass; //member class
virtual void f();
};

/* B inherits from A */
class B : public A {
public:
virtual void g (const myInt& i, const double& t); // myInt found
virtual void h (const myClass& m); // myClass found
};

/* Concrete implementation of interface A and B */
class C : public B {
public:
void f() { /* implementation */ }
void g(const myInt& i, const double& t) { /* implementation */ } // myInt found
void h(const myClass& m) { /* implementation */ } // myClass found
};
[/HTML]
Feb 16 '07 #4
rach
5
Once again, I found the problem does not relate to inheritance. Now I really found the solution to my problem. Everytime when you want to access a member type or a member class (nested class) in another class template, you have to use word typename and specify that class template. Even if your current class is derived from that class template, even if that member type or member class you want to access is public, you have to do so.

With normal class hierarchy, you don't need to worry about this.

Using the same example as above, here is an error-free version of the template hierarchy.

[HTML]
template <typename T>
class A {
public:
typedef int myInt; //member type
class myClass; //member class
virtual void f();
};

/* B inherits from A */
template <typename T>
class B : public A<T> {
public:
virtual void g (const typename A<T>::myInt& i, const T& t);
virtual void h (const typename A<T>::myClass& m);
};

/* Concrete implementation of interface A and B */
template <typename T>
class C : public B<T> {
public:
void f() { /* implementation */ }
void g(const typename A<T>::myInt& i, const T& t) { /* implementation */ }
void h(const typename A<T>::myClass& m) { /* implementation */ }
};
[/HTML]

Thank to this reference: http://www.thescripts.com/forum/thread588275.html
Feb 16 '07 #5

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

Similar topics

1
by: Stephane Ninin | last post by:
Hello all, I am trying to play with nested class in a script I am making, and I am not sure I really understand how they work. Here is some code: __all__ =
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
3
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
49
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.