472,328 Members | 1,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 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 1991
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...
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...
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,...
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...
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
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.