![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Friends in inheritance
Hello,
There is a rather annoying problem in my code, and I just can't get to the bottom of it. I have a class, A, and another one, B, which inherits from A. Now, A has a friend class, C, which is declared inside it. Whenever I want to use C's public constructor from within a method of B, I get errors like "expected type-specifier", and "expected ';'". I know friends are not inherited, so I made C a friend of B, too. Which changes absolutely nothing. Always the same errors. What am I doing wrong? If I call C's constructor like : A::C(...), or simply C(...), I get the same errors. Have any ideas? Thanks |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Friends in inheritance
My guess, not having seen code that illustrates the problem, is that you've left out a semi-colon or a closing brace somewhere in code before where the error is reported.
Or - less likely - you're working with template classes, for which the rules are slightly different, and you've left out a "typename" keyword at a point where you need it. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Friends in inheritance
Nice guess, I AM working with template classes. OK, here's the simplified code to illustrate what is happening :
cplusplus Syntax (Toggle Plain Text)
Now, here's the problem : cplusplus Syntax (Toggle Plain Text)
generates errors. So does : cplusplus Syntax (Toggle Plain Text)
EVEN IF, before declaring the class A, I declare B like so : cplusplus Syntax (Toggle Plain Text)
and put, in class C : cplusplus Syntax (Toggle Plain Text)
When using method1, I get the same errors. What should I do? How can B access C's constructor? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Friends in inheritance
It is usually better if you provide a SMALL but COMPLETE code sample that illustrates your problem. By paraphrasing, as you've done, you force people to guess at your problem because you may have left out key details.
My primary guess is that you've encountered one of the obscure limitations with templates in the current C++ standard -- fixing it is on the list of proposals for a future version of the standard. To make use of A<T>::C within a member function of B<T> you need to implement the member function inline (i.e. within the declaration of B<T>). For example; template <typename T> class B : public A<T> {
public:
int var;
void method() {A<T>::C x; var = x;}; // assumes A<T>::C can be implicitly converted to int
}; template <typename T> B<T>::method1(...) {
this->var = A<T>::C(...);
}But, in any event, the problem has nothing to do with friendship (friendship affects accessibility, not the sort of thing you're seeing). Without an actual code sample that illustrates your problem, I can't/won't guess further. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Friends in inheritance
First of all, thank you for your help.
Second, here is the actual code : cplusplus Syntax (Toggle Plain Text)
generates the following errors at 'new Element" : error : expected type-specifier before 'Element' And if I write : cplusplus Syntax (Toggle Plain Text)
i get: error : expected type-specifier So, my idea was to put cplusplus Syntax (Toggle Plain Text)
inside the class Element, and declaring DerivedList before LinkedList like so : cplusplus Syntax (Toggle Plain Text)
and the compiler gives the very same errors as before. What do you think? |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Friends in inheritance
Yep; you're running into the issue of dependant template types (instantiation of DerivedList template relies on instantiation of the LinkedList template, .... etc and the compiler is allowed to defer instantiation of any of those templates until it is too late).
In your particular case, a workaround I suggest is providing a copy constructor for LinkedList<> with the same body as your current DerivedList<> copy constructor, and not explicitly implement the DerivedList copy constructor. (the compiler generated copy constructor for DerivedList would automatically invoke the inherited one). Other than that, you need to inline ALL your definitions (no reliance on forward declaration of the Element class, no "out of line" implementation of copy constructors, etc). The philosophy is making sure that the compiler has access to all information it needs when .... eventually ... it finds it needs to instantiate templates. Friend declarations are not a solution to the problem: they just influence what code is allowed to access something. If it was, you could just make everything public and the problem would go away. More generically (C++ template issues aside) I'm failing to work out how your design makes sense for a linked list. Your LinkedList contains a pointer to an element, and presumably each element needs a pointer to the LinkedList that contains it. That circular dependancy does not make sense to me. Generally I would expect the nodes of a linked list (your Element) would contain data (the info member), and one or more pointers to other nodes. No need for a pointer back to the containing LinkedList. Practically, I'd also use a std::list<> anyway, rather than rolling my own linked list template as well. I assume you're doing this for learning purposes. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Friends in inheritance
Thank you ever so much for these tips.
As for your remarks about the implementation of the list, I agree with you. This code is not mine, it is given by an educator, who wants us to further enhance it. I know there are many much simpler ways (and more clearer ways) to implement a linked list, the aim of this exercise was to make us familiar with friends in inheritance. But you are absolutely right : this code is not efficient at all, and moreover, it is very unclear. Having said this, I thank you once more for outlining what should and should not be done when using template classes like this...so much I didn't know. |
|
|
|
|
|
#8 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Re: Friends in inheritance
I was playing around with writing data structures in C++ a while ago. I hope this is somewhat useful.
c++ Syntax (Toggle Plain Text)
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Friends in inheritance
Wow thanks! I'm sure I'll be able to put it to use!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Weird inheritance problem | Lakrids | C++ | 2 | Dec 23rd, 2007 6:24 AM |
| Inheritance and Object-Oriented Information Systems | emdiesse | Coder's Corner Lounge | 1 | Apr 24th, 2007 10:45 AM |
| Not class inheritance | Writlaus | C++ | 3 | Sep 2nd, 2006 7:44 PM |
| Reverse Inheritance | Darkhack | C# | 3 | Jan 16th, 2006 5:05 PM |
| Hello my (I Hope) Friends | Justin Case | Community Introductions | 16 | Sep 16th, 2005 4:08 AM |