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

K

kuangye

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top