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

friendship with nested class

I'm trying to declare the following friendship and VS.Net 2003 is
complaining:
template <class T>
class Outter
{
class Inner {...}
...
}

class A
{
template <class T> friend class Outter;
template <class T> friend class Outter<T>::Inner;
}
On the second friend declaration I get the error "'Outter::Inner'
cannot be redeclared in the current scope".
Any ideas? Is this a syntax issue or does the standard actually not
allow this?

Many thanks!!

Nov 25 '05 #1
4 2058
C++ compiler assumes that while accessing any member of the template
class, the class needs to be instantiated.
That's the type needs to known at compile time.

Ex.--

Try with something like.

friend class Outter<int>::Inner;

this works fine.

Nov 25 '05 #2
Sorry, I should have mentioned that this isn't possible because the
type for the template argument of Outter is unknown at this point :(
Technically I could forward declare every possibility and then declare
friendship with each but that's quite a hack and not terribly portable.

I was hoping there'd be some way of declaring friendship with Inner for
all template instantiations of Outter as is done with the first
friendship statement:

template <class T> friend class Outter;

Impossible????

Thx!

Nov 25 '05 #3
* Mr Dyl:
I'm trying to declare the following friendship and VS.Net 2003 is
complaining:
template <class T>
class Outter
{
class Inner {...}
...
}

class A
{
template <class T> friend class Outter;
template <class T> friend class Outter<T>::Inner;
}
On the second friend declaration I get the error "'Outter::Inner'
cannot be redeclared in the current scope".
Any ideas? Is this a syntax issue or does the standard actually not
allow this?


Since class Inner is private you can't refer to it. However, making it
public gives errors with MSVC 7.1 and MinGW g++ 3.4.4, while compiling
fine with Comeau Online 4.3.3.

The standard allows specifying a class nested in a template class as a
friend (of a non-template class), and even gives a direct example in
paragraph 14.5.3/6. That example, with a few callable functions added:

template<class T> struct A {
struct B { void foo(); }; // 'foo' added by me
void f();
};

class C {
template<class T> friend struct A<T>::B;
template<class T> friend void A<T>::f();

static void sayNoMore() {} // 'sayNoMore' added by me
};

// Following added by me:

template<class T> void A<T>::B::foo()
{
C::sayNoMore();
}

int main()
{
A<int>::B o;
o.foo();
}

This example from the standard has the same compilation problems as your
code did.

A workaround could perhaps be to move the nested class out, and if
necessary provide a typedef in the class it's nested in.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 25 '05 #4
Ed
You could make class A a template as well. This would allow you to
instantiate A with any type.

A<int> a1;
A<MyClass> a2
and so on.

template <typename T>
class A {
friend class Outer<T>;
friend class Outer<T>::Inner<T>;
};

Nov 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
3
by: jimmy | last post by:
Hi, I had a class Foo that grants friendship to a class FooFriend. I then wanted to use the pImpl idiom to FooFriend. So I have something like this, class FooFriend { private: class...
3
by: tom.s.smith | last post by:
I know that friendship isn't inherited in C++, and have two questions. (1) Why? Is it a technical problem, or just by design? If the latter, isn't this a little prescriptive, and shouldn't there...
2
by: Mark P | last post by:
Is there a way to do anything like the following: class A { friend void B::foo(); ... }; class B
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
2
by: Juha Nieminen | last post by:
Assume we have this kind of class hierarchy: class A { class B; }; class C { friend A;
6
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.