473,761 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using nested class, interface, inheritance gives error

5 New Member
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 3022
rach
5 New Member
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
EmptyContainerE xception(const string& err) : EmptyContainerE xception(err) by
EmptyContainerE xception(const string& err) : RuntimeExceptio n(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 Recognized Expert Specialist
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 New Member
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 New Member
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
1585
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
4030
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 Regards KInd --
11
2726
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
3677
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; but it complains for the following lines
3
2005
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 application. If you have not used the keyword 'Overridable' then read on for sure... If you setup a child class and you want to override a method in your base class, you may see the squiggles under your child's method name. The pop-up/build error says...
2
2374
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 when a nested class inherits from it’s parent class you get this infinite class chain in intellisense when consuming the class. To get around this I created two child classes Reader and Writer which require a base Person object. When consuming...
4
1844
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: C:\PK\Development\Products\UTCAS\4.0\SRC\MeltPracApplication\Dialog\Composit ionForm.cs(942): Argument '1': cannot convert from 'ref MeltPracData.MeltPracDataComposition' to 'ref MeltPracCommon.IDialogPostData'
49
5812
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 name search scope. i.e. a very simple application would be class ManyComputations { calling System.Math;
9
3471
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. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.