problem with a function template returning a private nested class ofa class template

Discussion in 'C++' started by ymost@hotmail.com, Dec 28, 2008.

  1. Guest

    Hi,
    I have a class template with two template arguments. It contains a
    private nested class, and I want a non-member function to return an
    instance of the nested class:

    //-----------------------------------------------------------------
    template<typename T1,typename T2> class A {
    class B {};
    };

    template<typename T> typename A<T,T>::B func(T x){};

    int main() {
    A<int,int> aii;
    A<double,double> add;
    A<int,double> aid;
    A<double,int> adi;
    func(1);
    return 0;
    }
    //----------------------------------------------------------------

    (I created some instances of A with different types for testing).
    Naturally I'm getting a 'class a<int, int>::B is private' error, but I
    simply cannot succeed in making func a friend function of the class
    template A. Each reference to func inside the class template A
    (whether it's a friend declaration or the whole function definition)
    causes either multiple ambiguous declarations of func, or an access
    violation because the different instances of A are not friends of each
    other (at least I think that's the reason).
    Help will be appreciated.
    Thanks
    , Dec 28, 2008
    #1
    1. Advertising

  2. Guest

    Re: problem with a function template returning a private nested classof a class template

    On Dec 27, 9:20 pm, wrote:
    > Hi,
    > I have a class template with two template arguments. It contains a
    > private nested class, and I want a non-member function to return an
    > instance of the nested class:
    >
    > //-----------------------------------------------------------------
    > template<typename T1,typename T2> class A {
    >   class B {};
    >
    > };
    >
    > template<typename T> typename A<T,T>::B func(T x){};
    >
    > int main() {
    >   A<int,int> aii;
    >   A<double,double> add;
    >   A<int,double> aid;
    >   A<double,int> adi;
    >   func(1);
    >   return 0;}
    >
    > //----------------------------------------------------------------
    >
    > (I created some instances of A with different types for testing).
    > Naturally I'm getting a 'class a<int, int>::B is private' error, but I
    > simply cannot succeed in making func a friend function of the class
    > template A. Each reference to func inside the class template A
    > (whether it's a friend declaration or the whole function definition)
    > causes either multiple ambiguous declarations of func, or an access
    > violation because the different instances of A are not friends of each
    > other (at least I think that's the reason).
    > Help will be appreciated.



    You have 3 options:

    1. Make the class public.

    2. Make the function a member function.

    3. Declare it as a friend, the syntax is:

    template<typename T1,typename T2> class A {
    class B {};
    template<typename T> friend typename A<T,T>::B func(T x);
    };

    Note that that declares func<int> to be a friend of A<float,double>,
    it's not completely strict about the types.

    As for your access violation, that's not related to "friend". "Friend"
    tells the compiler not to fail if the friend accesses a private/
    protected member of a class, but it's inconsequential to accessing
    memory outside of your program's boundaries.

    Jason
    , Dec 28, 2008
    #2
    1. Advertising

  3. Guest

    Re: problem with a function template returning a private nested classof a class template

    On Dec 28, 5:14 am, ""
    <> wrote:
    > On Dec 27, 9:20 pm, wrote:
    >
    >
    >
    > > Hi,
    > > I have a class template with two template arguments. It contains a
    > > private nested class, and I want a non-member function to return an
    > > instance of the nested class:

    >
    > > //-----------------------------------------------------------------
    > > template<typename T1,typename T2> class A {
    > >   class B {};

    >
    > > };

    >
    > > template<typename T> typename A<T,T>::B func(T x){};

    >
    > > int main() {
    > >   A<int,int> aii;
    > >   A<double,double> add;
    > >   A<int,double> aid;
    > >   A<double,int> adi;
    > >   func(1);
    > >   return 0;}

    >
    > > //----------------------------------------------------------------

    >
    > > (I created some instances of A with different types for testing).
    > > Naturally I'm getting a 'class a<int, int>::B is private' error, but I
    > > simply cannot succeed in making func a friend function of the class
    > > template A. Each reference to func inside the class template A
    > > (whether it's a friend declaration or the whole function definition)
    > > causes either multiple ambiguous declarations of func, or an access
    > > violation because the different instances of A are not friends of each
    > > other (at least I think that's the reason).
    > > Help will be appreciated.

    >
    > You have 3 options:
    >
    > 1. Make the class public.
    >
    > 2. Make the function a member function.
    >
    > 3. Declare it as a friend, the syntax is:
    >
    >   template<typename T1,typename T2> class A {
    >     class B {};
    >     template<typename T> friend typename A<T,T>::B func(T x);
    >   };
    >
    > Note that that declares func<int> to be a friend of A<float,double>,
    > it's not completely strict about the types.
    >
    > As for your access violation, that's not related to "friend". "Friend"
    > tells the compiler not to fail if the friend accesses a private/
    > protected member of a class, but it's inconsequential to accessing
    > memory outside of your program's boundaries.
    >
    > Jason


    This solves my problem, thanks!
    , Dec 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaveLessnau
    Replies:
    3
    Views:
    417
    Howard
    May 16, 2005
  2. David

    I/O ofa large number of files

    David, Aug 1, 2005, in forum: C Programming
    Replies:
    5
    Views:
    361
    Jasen Betts
    Aug 4, 2005
  3. Replies:
    15
    Views:
    1,771
  4. Replies:
    3
    Views:
    570
    Bertrand
    Jan 10, 2009
  5. bart plessers

    dbase: how to get fieldNAME ofa table

    bart plessers, Oct 2, 2003, in forum: ASP General
    Replies:
    4
    Views:
    150
    Bob Barrows
    Oct 2, 2003
Loading...

Share This Page