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. Advertising

  2. kuangye

    kuangye Guest

    declare the whole template as friend...
    kuangye, Jun 26, 2008
    #2
    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. Art
    Replies:
    2
    Views:
    1,214
    Terry Reedy
    Jun 24, 2009
  2. Ned Batchelder
    Replies:
    0
    Views:
    86
    Ned Batchelder
    Dec 16, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    69
    Chris Angelico
    Dec 16, 2013
  4. Devin Jeanpierre
    Replies:
    0
    Views:
    74
    Devin Jeanpierre
    Dec 16, 2013
  5. Steven D'Aprano
    Replies:
    10
    Views:
    127
    Gregory Ewing
    Dec 18, 2013
Loading...

Share This Page