construct a new type obj failed---how to walk around it...

Discussion in 'C++' started by kuangye, Jun 25, 2008.

  1. kuangye

    kuangye Guest

    Hi, all.

    ///////////////////////////////////
    ////////code sample
    /////compiling error in vs2005 and gcc3.4
    ///////////////////////////////////
    class CB;

    CB fn(CB& obj);

    class CB
    {
    protected:
    CB(){}
    CB(const CB& ob){}

    friend CB fn(CB& obj);
    };

    CB fn(CB& obj)
    {
    return CB();
    }

    //////////////////////////////////////
    template<typename T>
    class TA;

    template<typename T>
    TA<int> toolfn(TA<T>& obj);

    template<typename T>
    class TA:public CB
    {
    template<typename U>
    friend class TA;

    public:
    TA(){};
    protected:
    //should be protected or private
    TA(const CB& ob):CB(ob){}

    public:
    friend TA<int> toolfn<T>(TA<T>& obj);
    };


    template<typename T>
    TA<int> toolfn(TA<T>& obj)
    {
    return TA<int>( fn(obj) );
    }

    int main()
    {
    TA<float> f1;
    TA<int> ret = toolfn(f1);//----->how to make it pass the compiling.
    /*
    compiling error:
    1.cpp: In function `TA<int> toolfn(TA<T>&) [with T = float]':
    1.cpp:52: instantiated from here
    1.cpp:36: error: `TA<T>::TA(const CB&) [with T = int]' is protected
    1.cpp:46: error: within this context
    */

    return 0;
    }

    ////////////////////////////////////////////
    ///currently, i can using a helper function to walk around this
    //but it seems that it does not elegant enough...
    ///////////////////////////////

    Any hint will be appreciated
     
    kuangye, Jun 25, 2008
    #1
    1. Advertisements

  2. kuangye

    kuangye Guest

    declare the whole template as friend...
     
    kuangye, Jun 26, 2008
    #2
    1. Advertisements

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. Mark Kamoski
    Replies:
    3
    Views:
    16,492
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    750
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    583
    Mike C. Fletcher
    Feb 20, 2004
  4. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    740
    Peter Otten
    May 4, 2005
  5. Marcus Alves Grando
    Replies:
    7
    Views:
    673
    Marcus Alves Grando
    Nov 14, 2007
  6. Art
    Replies:
    2
    Views:
    1,490
    Terry Reedy
    Jun 24, 2009
  7. Mukesh
    Replies:
    4
    Views:
    835
    Paul N
    Mar 26, 2010
  8. Steven D'Aprano
    Replies:
    10
    Views:
    316
    Gregory Ewing
    Dec 18, 2013
Loading...